Examples of creating, querying, and modifying XML files with namespaces

Source: Internet
Author: User
Tags add dotnet string uuid xmlns
xml| Create

XML is becoming more and more widely used, such as Vista, Flex programming will use XML, correctly grasp the various operations of XML, to improve the efficiency of programming is critical. Here is an example of an integrated approach to XML with namespaces.
C#:string w3namespace = "http://www.w3.org/2000/xmlns/";
System.Xml.XmlDocument doc = new System.Xml.XmlDocument ();

To create a root node
System.Xml.XmlNode root = Doc. CreateNode (System.Xml.XmlNodeType.Element, "w", "Worddocument", "http://schemas.microsoft.com/office/word/2003/2/wordml");
System.Xml.XmlAttribute xa;
Xa = doc. CreateAttribute ("xmlns", "V", w3namespace);
Xa. Value = "URN:SCHEMAS-MICROSOFT-COM:VML";
Root. Attributes.append (XA);

Adding attributes to a node
Xa = doc. CreateAttribute ("xmlns", "W10", w3namespace);
Xa. Value = "Urn:schemas-microsoft-com:office:word";
Root. Attributes.append (XA);

Xa = doc. CreateAttribute ("xmlns", "SL", w3namespace);
Xa. Value = "Http://schemas.microsoft.com/schemaLibrary/2003/2/core";
Root. Attributes.append (XA);

Xa = doc. CreateAttribute ("xmlns", "AML", W3namespace);
Xa. Value = "Http://schemas.microsoft.com/aml/2001/core";
Root. Attributes.append (XA);

Xa = doc. CreateAttribute ("xmlns", "wx", w3namespace);
Xa. Value = "Http://schemas.microsoft.com/office/word/2003/2/auxHint";
Root. Attributes.append (XA);

Xa = doc. CreateAttribute ("xmlns", "O", w3namespace);
Xa. Value = "Urn:schemas-microsoft-com:office:office";
Root. Attributes.append (XA);

Xa = doc. CreateAttribute ("xmlns", "DT", w3namespace);
Xa. Value = "uuid:c2f41010-65b3-11d1-a29f-00aa00c14882";
Root. Attributes.append (XA);

Xa = doc. CreateAttribute ("xmlns", "space", w3namespace);
Xa. Value = "preserve";
Root. Attributes.append (XA);

Add a value to a node
System.Xml.XmlNode BODY = doc. CreateNode (System.Xml.XmlNodeType.Element, "V", "body", "urn:schemas-microsoft-com:vml");
System.Xml.XmlNode Childnode = doc. CreateNode (System.Xml.XmlNodeType.Element, "O", "T", "Urn:schemas-microsoft-com:office:office");
Childnode.innertext = "Welcome to the wonderful world of Mengxian" ";

Add to the memory tree
Body. AppendChild (Childnode);
Root. AppendChild (body);
Doc. AppendChild (root);

Add Node Declaration
System.Xml.XmlDeclaration xd = doc. Createxmldeclaration ("1.0", "UTF-8", "yes");
Doc. InsertBefore (XD, Doc. DocumentElement);

Add processing instructions
System.Xml.XmlProcessingInstruction SPI = doc. Createprocessinginstruction ("Mso-application", "progid=" Word.Document ")";
Doc. InsertBefore (SPI, Doc. DocumentElement);

Query node
System.Xml.XmlNamespaceManager Nsmanager = new System.Xml.XmlNamespaceManager (Doc. NameTable);
Nsmanager. AddNamespace ("W", "Http://schemas.microsoft.com/office/word/2003/2/wordml");
Nsmanager. AddNamespace ("V", "urn:schemas-microsoft-com:vml");
Nsmanager. AddNamespace ("O", "Urn:schemas-microsoft-com:office:office");
System.Xml.XmlNode node = doc. selectSingleNode ("W:worddocument/v:body/o:t", Nsmanager);
Response.Write (node. InnerText);

Node. InnerText = "Welcome to the wonderful World of Mengxian": http://dotnet.aspx.cc/";

To create a CDATA node
System.Xml.XmlCDataSection Xcds = doc. Createcdatasection ("<a href= ' http://dotnet.aspx.cc/' >" Mengxian the Wonderful World "</a>");
Node. Parentnode.insertafter (Xcds, node);

Response.Write (Xcds. InnerText);

Doc. Save (Server.MapPath ("Test.xml"));
Vb. Net:dim w3namespace as String = "http://www.w3.org/2000/xmlns/"
Dim doc as System.Xml.XmlDocument = New System.Xml.XmlDocument
Dim root as System.Xml.XmlNode = doc. CreateNode (System.Xml.XmlNodeType.Element, "w", "Worddocument", "HTTP://SCHEMAS.MICROSOFT.COM/OFFICE/WORD/2003/2/WORDML")
Dim XA as System.Xml.XmlAttribute
Xa = doc. CreateAttribute ("xmlns", "V", W3namespace)
Xa. Value = "URN:SCHEMAS-MICROSOFT-COM:VML"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "W10", W3namespace)
Xa. Value = "Urn:schemas-microsoft-com:office:word"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "SL", W3namespace)
Xa. Value = "Http://schemas.microsoft.com/schemaLibrary/2003/2/core"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "AML", W3namespace)
Xa. Value = "Http://schemas.microsoft.com/aml/2001/core"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "wx", W3namespace)
Xa. Value = "Http://schemas.microsoft.com/office/word/2003/2/auxHint"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "O", W3namespace)
Xa. Value = "Urn:schemas-microsoft-com:office:office"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "DT", W3namespace)
Xa. Value = "uuid:c2f41010-65b3-11d1-a29f-00aa00c14882"
Root. Attributes.append (XA)
Xa = doc. CreateAttribute ("xmlns", "space", W3namespace)
Xa. Value = "preserve"
Root. Attributes.append (XA)
Dim body as System.Xml.XmlNode = doc. CreateNode (System.Xml.XmlNodeType.Element, "V", "body", "URN:SCHEMAS-MICROSOFT-COM:VML")
Dim Childnode as System.Xml.XmlNode = Doc. CreateNode (System.Xml.XmlNodeType.Element, "O", "T", "Urn:schemas-microsoft-com:office:office")
Childnode.innertext = "Welcome to the wonderful world of Mengxian" "
Body. AppendChild (Childnode)
Root. AppendChild (body)
Doc. AppendChild (Root)
Dim xd as System.Xml.XmlDeclaration = doc. Createxmldeclaration ("1.0", "UTF-8", "yes")
Doc. InsertBefore (XD, Doc. documentelement)
Dim SPI as System.Xml.XmlProcessingInstruction = doc. Createprocessinginstruction ("Mso-application", "progid=" "Word.Document" ")
Doc. InsertBefore (SPI, Doc. documentelement)
Dim Nsmanager as System.Xml.XmlNamespaceManager = New System.Xml.XmlNamespaceManager (Doc. NameTable)
Nsmanager. AddNamespace ("W", "Http://schemas.microsoft.com/office/word/2003/2/wordml")
Nsmanager. AddNamespace ("V", "urn:schemas-microsoft-com:vml")
Nsmanager. AddNamespace ("O", "Urn:schemas-microsoft-com:office:office")
Dim node as System.Xml.XmlNode = Doc. selectSingleNode ("W:worddocument/v:body/o:t", Nsmanager)
Response.Write (node. InnerText)
Node. InnerText = "Welcome to the wonderful World of Mengxian": http://dotnet.aspx.cc/"
Dim Xcds as System.Xml.XmlCDataSection = Doc. Createcdatasection ("<a href= ' http://dotnet.aspx.cc/' >" Mengxian's Wonderful World "</a>")
Node. Parentnode.insertafter (Xcds, node)
Response.Write (Xcds. InnerText)
Doc. Save (Server.MapPath ("Test.xml"))



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.