HOW TO: Implement Common MSXML Tasks in System.xml By Using Visual C# .NET (330589)
The information in this article applies to:
- Microsoft .NET Framework Class Libraries 1.1
- Microsoft .NET Framework Class Libraries 1.0
- Microsoft Visual C# .NET (2003)
- Microsoft Visual C# .NET (2002)
- Microsoft XML 4.0
- Microsoft Visual Basic Enterprise Edition for Windows 6.0
This article was previously published under Q330589 This article refers to the following Microsoft .NET
Framework Class Library namespaces:
- System.Xml
- System.Xml.Xsl
- System.Xml.XPath
- System.Net
IN THIS TASKSUMMARYThis step-by-step article describes the features of MSXML
and .NET Framework Classes for XML processing. This article also includes
samples on how to use MSXML in Microsoft Visual Basic 6.0 and how to use .NET
Framework Classes for XML processing.
- Microsoft XML Core Services (MSXML)
Using MSXML, you can build XML-based applications. You
can easily use MSXML in Visual Studio 6.0 applications as DOM and SAX Parser
with support for XSLT and XPath. - .NET Framework Classes for XML
The System.Xml namespace provides standards-based support for processing XML. System.xml is not just the managed version of MSXML; its functionality may
overlap that of the MSXML COM library, and it also contains a rich object model
and hierarchy.
For additional information, visit the following Microsoft Web
site: Design Goals for XML in the .NET Framework back to the
topLoad, Access, Manipulate, and Save XML DocumentsThe following code example narrates how to load and save the XML
documents. The sample also includes how to append, delete, and update the
elements in XML documents. Using MSXMLNote: To run the following MSXML sample code, see the "Requirements"
section in this article: On Error GoTo Handle
'Create the XmlDocument.
Dim objDOMDoc As New DOMDocument30
objDOMDoc.async = False
objDOMDoc.Load ("C:\XMLMigration\books.xml")
Debug.Print ("\n XML Document Loaded")
' Set the root element.
Dim root As MSXML2.IXMLDOMElement
Set root = objDOMDoc.documentElement
' Select and display the value of all the publicationdate attributes.
Debug.Print ("\n List of Publications")
Dim pbDateList As MSXML2.IXMLDOMNodeList
Set pbDateList = root.selectNodes("/bookstore/book/@publicationdate")
Dim nameList As MSXML2.IXMLDOMNodeList
Set nameList = root.selectNodes("/bookstore/book/title")
Dim i As Integer
For i = 0 To nameList.length - 1 Step i + 1
Debug.Print ("\n\t\tName: " + nameList(i).Text + ",Published :" + pbDateList(i).Text)
Next
'Select the book node with the matching attribute value.
Dim book As MSXML2.IXMLDOMNode, price As MSXML2.IXMLDOMNode
Set book = root.selectSingleNode("book[@bk:ISBN='1-861001-57-6']")
' Selects the first 'Price' element and modify its value.
Set price = book.selectSingleNode("price")
price.Text = "50.5"
Debug.Print ("\n First Book Price Updated")
' Remove the last element.
root.removeChild root.lastChild
Debug.Print ("\n Last Element Deleted")
'Create a new node.
Dim elem As MSXML2.IXMLDOMElement
Set elem = objDOMDoc.createElement("book")
elem.setAttribute "genre", "novel"
elem.setAttribute "publicationdate", "1982"
' Attribute to the root namespace (another namespace)
Dim objAttribNode As MSXML2.IXMLDOMNode
Set objAttribNode = objDOMDoc.createNode(2, "bk:ISBN", "urn:samples")
objAttribNode.nodeTypedValue = "1-851002-30-1"
elem.Attributes.setNamedItem objAttribNode
'Add the node to the document.
root.appendChild elem
Debug.Print ("\n Element appended to XML Document")
objDOMDoc.save ("C:\XMLMigration\books.xml")
Debug.Print ("\n XML Document Saved")
Exit Sub
Handle:
Debug.Print ("Error: " + Err.Description)
Using .NET Framework Classes
using System;
using System.Xml;
public class CSharpSample
{
public static void Main()
{
//Create the XmlDocument.
Console.Write ("\n .NET Framework Sample\n");
XmlDocument doc = new XmlDocument();
doc.Load(@"C:\XMLMigration\books.xml");
//Create an XmlNamespaceManager for resolving namespaces.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk","urn:samples");
XmlNode book,price;
XmlElement root = doc.DocumentElement;
// Select and display the value of all the publicationdate attributes.
Console.Write ("\n List of Publications\n");
XmlNodeList pbDateList = root.SelectNodes("/bookstore/book/@publicationdate");
XmlNodeList nameList = root.SelectNodes("/bookstore/book/title");
for(int i=0;i < nameList.Count ;i++)
Console.WriteLine("\n\t\tName: " + nameList[i].InnerText + ",Published :" + pbDateList[i].InnerText);
//Select the book node with the matching attribute value.
book = root.SelectSingleNode("descendant::book[@bk:ISBN='1-861001-57-6']", nsmgr);
// Selects the first 'Price' element and modify its value.
price = book.SelectSingleNode("price");
price.InnerText = "50.5";
Console.Write ("\n First Book Price Updated");
// Remove the first element.
root.RemoveChild(root.LastChild);
Console.Write ("\n Last Element Deleted");
// Create a new node and set its attributes.
XmlElement elem = doc.CreateElement("book");
elem.SetAttribute("genre","novel");
elem.SetAttribute("publicationdate","1982");
elem.SetAttribute("ISBN","urn:samples","1-851002-30-1");
//Add the node to the document and save the XML.
root.AppendChild(elem);
Console.Write ("\n Element appended to XML Document");
doc.Save(@"C:\XMLMigration\books.xml");
Console.Write ("\n XML Document Saved");
Console.ReadLine();
}
}
Using XPathDocument for Read-only XPath QueriesTo perform XPath queries in read-only mode, Microsoft recommends
that you use the System.Xml.XPath.XPathDocument class. The following example describes how to use XPathDocument class to execute the XPath queries. using System;
using System.Xml;
using System.Xml.XPath;
public class XPATHDOC
{
public static void Main()
{
// Creating XPathDocument and XPathNavigator.
XPathDocument doc = new XPathDocument(@"C:\XMLMigration\books.xml");
XPathNavigator nav = doc.CreateNavigator();
// Creating XPathExpression to select all the novels
XPathExpression expression = nav.Compile("descendant::book[@genre='novel']");
XPathNodeIterator nodeIterator = nav.Select(expression);
while (nodeIterator.MoveNext())
{
XPathNavigator nav2 = nodeIterator.Current;
Console.Write("\n\t Book Title & Price : " + nav2.Value);
nav2.MoveToNext();
}
}
}
Back to the
topREFERENCES For additional
information about the System.XML namespace , click the following article
numbers to view the articles in the Microsoft Knowledge Base: 313824
INFO: Roadmap for Programming XML with the DOM-Model Parser in the .NET
Framework.
313826 INFO: Roadmap for XML Schemas in the .NET Framework
313651 INFO:
Roadmap for XML in the .NET Framework.
318499 HOW TO: Use
the System.Xml.XmlDocument Class to Execute XPath Queries in Visual C#
.NET
back to the top
Modification Type: | Minor | Last Reviewed: | 8/4/2004 |
---|
Keywords: | kbdownload kbXML kbValidation kbHOWTOmaster KB330589 kbAudDeveloper |
---|
|