Amazing User
Amazing User

Reputation: 3563

Checking the validity of XML code

Need to verify the XML:

I think that I need to create XDocument like:

static void Main(string[] args)
{
    string s = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
               "<!--This is a comment.-->" +
               "<?xml-stylesheet href='mystyle.css' title='Compact' type='text/css'?>" +
               "<Pubs>" +
                   "<Book>" +
                       "<Title>Artifacts of Roman Civilization</Title>" +
                       "<Author>Moreno, Jordao</Author>" +
                   "</Book>" +
                   "<Book>" +
                       "<Title>Midieval Tools and Implements</Title>" +
                       "<Author>Gazit, Inbar</Author>" +
                   "</Book>" +
               "</Pubs>" +
               "<!--This is another comment.-->";//= Console.ReadLine();
    try
    {
        XDocument xDoc = XDocument.Parse(s);                
        xDoc.Save("C://22.xml");
        Console.WriteLine("Valid");
    }
    catch
    {
        Console.WriteLine("Invalid");
    }
}

Is there anything similar XDocument.Parse(s) for Framework 2?

Upvotes: 0

Views: 52

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125630

XDocument and entire LINQ to XML was introduced in .NET 3.5

If you're using .NET Framework 2.0, you should use XmlDocument:

var doc = new XmlDocument();
doc.LoadXml(s);
doc.Save("C//22.xml");

LoadXml throws XmlException when document is invalid and parsing cannot be performed.

Upvotes: 1

Related Questions