Click here to Skip to main content
15,886,788 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi
how can i check whether user enter xmlstring is in this below format or not .if the string was not in this format i have throw an error saying invalid xml format

XML
<RPM>
<AI ID="1918" user="xyzs " Act="T"  />
</RPM>
Posted

You should create an XSD schema that defines your XML format.

Then, when processing incoming XML, you can check it against the defined schema to ensure in conforms to the specification.

The Validation Event [^] would let you report on which sections of the XML are invalid according to your schema.
 
Share this answer
 
Comments
VJ Reddy 21-May-12 4:58am    
Good answer. 5!
lakshmichawala 21-May-12 6:06am    
hi iam new to xsd preparation will u be give help in this regard
Dylan Morley 21-May-12 6:11am    
Use the XSD tool to create an XML schema from your XML file - http://msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.80%29.aspx. Once you've got an XSD file, you can just use an XmlReader with some flags set to make it validate against an XSD.

http://stackoverflow.com/questions/751511/validating-an-xml-against-referenced-xsd-in-c-sharp

Have a google, follow the links - the answers are all there,
The Solution 1 given by Dylan Morley is a good answer.

In case you want to check the above format without XSD schema then I think Regex can be used with the following format
C#
string xmlString =@"<RPM>
        <AI ID=""1918"" user=""xyzs "" Act=""T""  />
        </RPM>";
bool isValid = Regex.IsMatch(xmlString, @"<RPM>\s*<AI\s+ID\s*=\s*""\d+""\s+" +
        @"user\s*=\s*""[a-z]+\s*""\s+Act\s*=\s*""[A-Z]""\s*/>\s*</RPM>",
        RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | 
        RegexOptions.Multiline);

The Regex pattern can be tested here
http://regexhero.net/tester/[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900