Vaibhav Jain
Vaibhav Jain

Reputation: 34407

Convert string in xml document

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

Answers (3)

Kamran Khan
Kamran Khan

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

UVM
UVM

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

Related Questions