What is the best way to deal with XML documents, XSD etc in C# 2.0?
Which classes to use etc. What are the best practices of parsing and making XML documents etc.
EDIT: .Net 3.5 suggestions are also welcome.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The primary means of reading and writing in C# 2.0 is done through the XmlDocument class. You can load most of your settings directly into the XmlDocument through the XmlReader it accepts.
Loading XML Directly
Loading XML From a File
I find the easiest/fastest way to read an XML document is by using XPath.
Reading an XML Document using XPath (Using XmlDocument which allows us to edit)
If you need to work with XSD documents to validate an XML document you can use this.
Validating XML Documents against XSD Schemas
Validating XML against XSD at each Node (UPDATE 1)
Writing an XML Document (manually)
(UPDATE 1)
In .NET 3.5, you use XDocument to perform similar tasks. The difference however is you have the advantage of performing Linq Queries to select the exact data you need. With the addition of object initializers you can create a query that even returns objects of your own definition right in the query itself.
(UPDATE 2)
A nice way in .NET 3.5 is to use XDocument to create XML is below. This makes the code appear in a similar pattern to the desired output.
creates
All else fails, you can check out this MSDN article that has many examples that I’ve discussed here and more. http://msdn.microsoft.com/en-us/library/aa468556.aspx