This is the fourth day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021
C#’s XmlDocument class is an in-memory representation of an XML document. It implements a W3C level 1 core and a core DOM level 2.
XML Document Object Model (DOM)
The XML Document Object Model (DOM) class is an in-memory representation of an XML document. The DOM enables you to programmatically read, process, and modify XML documents. Although the XmlReader class also reads XML, it provides uncached, forward-only read-only access. That is, you cannot edit attribute values or element content, or insert and delete nodes using XmlReader. Editing is the main function of the DOM. The in-memory representation of XML data is a common structured approach, although the actual XML data is stored in a linear fashion as it is passed in a file or from another object. Here is the XML data.
<? The XML version = "1.0"? > <books> <book> <author> <price format="dollar">31.95</price> <pubdate>05/01/2001</pubdate> </book> <pubinfo> <publisher>MSPress</publisher> <state>WA</state> </pubinfo> </books>Copy the code
How memory is constructed when XML data is read into a DOM structure.
Construct XML using XmlDocument
We built the above document using the XmlDocument class.
XmlDocument xmlDoc = new XmlDocument(); XmlDoc. CreateXmlDeclaration (" 1.0 ", "utf-8", "yes"); XmlElement xmlRootNode = xmlDoc.CreateElement("books"); XmlElement bookNode = xmlDoc.CreateElement("book"); XmlElement authorNode = xmlDoc.CreateElement("author"); authorNode.InnerText="Carson"; bookNode.AppendChild(authorNode); XmlElement authorNode = xmlDoc.CreateElement("author"); authorNode.InnerText="Carson"; bookNode.AppendChild(authorNode); XmlElement priceNode = xmlDoc.CreateElement("price"); AuthorNode. The InnerText = "31.95"; authorNode.SetAttribute("format","dollar"); bookNode.AppendChild(priceNode); XmlElement pubdateNode = xmlDoc.CreateElement("pubdate"); pubdateNode.InnerText="05/01/2001"; bookNode.AppendChild(pubdateNode); xmlRootNode.AppendChild(bookNode); XmlElement pubinfoNode = xmlDoc.CreateElement("pubinfo"); XmlElement publisherNode = xmlDoc.CreateElement("publisher"); publisherNode.InnerText="MSPress"; pubinfoNode.AppendChild(publisherNode); XmlElement stateNode = xmlDoc.CreateElement("state"); stateNode.InnerText="WA"; pubinfoNode.AppendChild(stateNode); xmlRootNode.AppendChild(pubinfoNode);Copy the code
Initialize the document using XmlDocument, and then CreateXmlDeclaration() sets the XML header. Finally, the nodes are organized into an XML structure with AppendChild.
Store and get XML text
The xmldoc.save (path) method saves the XML document directly to a file by passing in the path of the file to be saved. If you want to get the XML directly, you also need to use XmlWriter to write the XML and get the text.
XmlWriter xmlWriter = null; XmlWriterSettings settings = new XmlWriterSettings(); Settings. Indent = true; // Set encoding to utF-8. By default, utF-16 settings. encoding = new UTF8Encoding(false); // Set the NewLine character settings.NewLineChars = environment. NewLine; MemoryStream ms = new MemoryStream(); try { xmlWriter = XmlWriter.Create(ms, settings); xmlWriter.WriteStartDocument(); xmlDoc.WriteTo(xmlWriter); } finally { if (xmlWriter ! = null) xmlWriter.Close(); ms.Close(); } string xmlStr=Encoding.UTF8.GetString(ms.ToArray());Copy the code
So we can get the text of the XML and store it directly into the database field. There are other ways to read and write XML, such as using XmlWriter directly. Next time, XmlWriter.