The main readers and writers in C # 2.0 are executed using the XmlDocument class. You can load most of your settings directly into the XmlDocument through the XmlReader that it accepts.
XML loading directly
XmlDocument document = new XmlDocument(); document.LoadXml("<People><Person Name='Nick' /><Person Name='Joe' /></People>");
Loading XML from a file
XmlDocument document = new XmlDocument(); document.Load(@"C:\Path\To\xmldoc.xml"); // Or using an XmlReader/XmlTextReader XmlReader reader = XmlReader.Create(@"C:\Path\To\xmldoc.xml"); document.Load(reader);
I find the easiest / fastest way to read an XML document using XPath.
Reading an XML document using XPath (using an XmlDocument that allows us to edit)
XmlDocument document = new XmlDocument(); document.LoadXml("<People><Person Name='Nick' /><Person Name='Joe' /></People>"); // Select a single node XmlNode node = document.SelectSingleNode("/People/Person[@Name = 'Nick']"); // Select a list of nodes XmlNodeList nodes = document.SelectNodes("/People/Person");
If you need to work with XSD documents to validate an XML document, you can use this.
Validating XML documents against XSD schemas
XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidateType = ValidationType.Schema; settings.Schemas.Add("", pathToXsd); // targetNamespace, pathToXsd XmlReader reader = XmlReader.Create(pathToXml, settings); XmlDocument document = new XmlDocument(); try { document.Load(reader); } catch (XmlSchemaValidationException ex) { Trace.WriteLine(ex.Message); }
XML Validation on XSD on Each Node (UPDATE 1)
XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidateType = ValidationType.Schema; settings.Schemas.Add("", pathToXsd); // targetNamespace, pathToXsd settings.ValidationEventHandler += new ValidationEventHandler(settings_ValidationEventHandler); XmlReader reader = XmlReader.Create(pathToXml, settings); while (reader.Read()) { } private void settings_ValidationEventHandler(object sender, ValidationEventArgs args) { // e.Message, e.Severity (warning, error), e.Error // or you can access the reader if you have access to it // reader.LineNumber, reader.LinePosition.. etc }
Writing an XML Document (Manual)
XmlWriter writer = XmlWriter.Create(pathToOutput); writer.WriteStartDocument(); writer.WriteStartElement("People"); writer.WriteStartElement("Person"); writer.WriteAttributeString("Name", "Nick"); writer.WriteEndElement(); writer.WriteStartElement("Person"); writer.WriteStartAttribute("Name"); writer.WriteValue("Nick"); writer.WriteEndAttribute(); writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush();
(UPDATE 1)
In .NET 3.5, you use XDocument to perform similar tasks. The difference, however, is that you have the advantage of executing 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 directly in the query itself.
XDocument doc = XDocument.Load(pathToXml); List<Person> people = (from xnode in doc.Element("People").Elements("Person") select new Person { Name = xnode.Attribute("Name").Value }).ToList();
(UPDATE 2)
A good way in .NET 3.5 is to use an XDocument to create the XML below. This leads to the fact that the code looks the same way with the desired output.
XDocument doc = new XDocument( new XDeclaration("1.0", Encoding.UTF8.HeaderName, String.Empty), new XComment("Xml Document"), new XElement("catalog", new XElement("book", new XAttribute("id", "bk001"), new XElement("title", "Book Title") ) ) );
creates
<catalog> <book id="bk001"> <title>Book Title</title> </book> </catalog>
Everything else fails, you can check out this MSDN article, which has a lot of examples that I discussed here and more. http://msdn.microsoft.com/en-us/library/aa468556.aspx