Reputation: 34407
I am getting some xml in a string variable through a wcf webservice. I need to confirm that the string xml I am getting is a valid xml or not.
and I would also like to convert this string to xml document for further processing. Please let me know how to do it.
Upvotes: 0
Views: 5645
Reputation: 9986
How about using XDocument.Parse()
string str =
@"<?xml version=""1.0""?>
<!-- comment at the root level -->
<Root>
<Child>Content</Child>
</Root>";
XDocument doc = XDocument.Parse(str);
Console.WriteLine(doc);
Or if you want to catch the parsing error, use try/catch:
try {
XElement contacts = XElement.Parse(
@"<Contacts>
<Contact>
<Name>Jim Wilson</Name>
</Contact>
</Contcts>");
Console.WriteLine(contacts);
}
catch (System.Xml.XmlException e)
{
Console.WriteLine(e.Message);
}
Upvotes: 2
Reputation: 9914
I think you can validate the wcf response xml with MessageInspector.MSDN contains a worked example of how this can be accomplished using WCF MessageInspectors
Upvotes: 0
Reputation: 554
try to use XElement.Parse : http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.parse.aspx
Upvotes: 0