Example of creating, querying, and modifying an XML file with a namespace in asp.net

Source: Internet
Author: User
Tags dotnet processing instruction

C #:

String w3NameSpace = "http://www.w3.org/2000/xmlns ";
System. Xml. XmlDocument doc = new System. Xml. XmlDocument ();

// Create the 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 );

// Add attributes for the 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 ";
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 value to the 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 ");
ChildNode. InnerText = "Welcome to [wonderful world of the mengxian meeting ]";

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

// Add a node Declaration
System. Xml. XmlDeclaration xd = doc. CreateXmlDeclaration ("1.0", "UTF-8", "yes ");
Doc. InsertBefore (xd, doc. DocumentElement );

// Add a Processing Instruction
System. Xml. XmlProcessingInstruction spi = doc. CreateProcessingInstruction ("mso-application", "progid = \" Word. Document \"");
Doc. InsertBefore (spi, doc. DocumentElement );

// Query nodes
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 ");
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 Meng xianhui]: http://dotnet.aspx.cc /";

// Create a CDATA Node
System. xml. xmlCDataSection xcds = doc. createCDataSection ("<a href = 'HTTP: // dotnet. aspx. cc/'> [wonderful world of Meng xianhui] </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 New System. Xml. XmlDocument

'Create Root Node
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)

'Add 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"
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)

'Is the added value of the node.
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 ")
ChildNode. InnerText = "Welcome to [wonderful world of the mengxian meeting ]"

'Add to memory tree
Body. AppendChild (childNode)
Root. AppendChild (body)
Doc. AppendChild (root)

'Add a node Declaration
Dim xd As System. Xml. XmlDeclaration = doc. CreateXmlDeclaration ("1.0", "UTF-8", "yes ")
Doc. InsertBefore (xd, doc. DocumentElement)

'Add Processing Instruction
Dim spi As System. Xml. XmlProcessingInstruction = doc. CreateProcessingInstruction ("mso-application", "progid =" "Word. Document """)
Doc. InsertBefore (spi, doc. DocumentElement)

'Query a node
Dim nsmanager As 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 ")
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 Meng xianhui": http://dotnet.aspx.cc /"

'Create a CDATA Node
Dim xcds As System. xml. xmlCDataSection = doc. createCDataSection ("<a href = 'HTTP: // dotnet. aspx. cc/'> [wonderful world of Meng xianhui] </a> ")
Node. ParentNode. InsertAfter (xcds, node)

Response. Write (xcds. InnerText)

Doc. Save (Server. MapPath ("test. xml "))

Related Article

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.