1.
using System.XML
XmlNodeList nodelist = xmlDocument.DocumentElement.GetElementsByTagName("SharesStockCode");
2
<?xml version="1.0" encoding="utf-8" ?>
<Clients>
<Client> <ClientID>Merri</ClientID>
<ClientName>Merrill </ClientName>
<className>Merril</className>
<FileFilter>(*.csv)|*.csv|(*.pdf)|*.pdf</FileFilter>
<FileDefaultExt>.csv|.pdf</FileDefaultExt>
<FileType>CSV|PDF</FileType>
<OutPutFileType>PDF</OutPutFileType>
</Client>
<Client>
<ClientID>Credit</ClientID>
<ClientName>Credit </ClientName>
<className>CreditS</className>
<FileFilter>(*.doc)|*.doc</FileFilter>
<FileDefaultExt>.doc</FileDefaultExt>
<FileType>Word</FileType>
<OutPutFileType>PDF|CD001</OutPutFileType>
</Client>
</Clients>
using System.XML.Ling;
例如:
XDocument xDocument = XDocument.Load(clientInformationFilePath);
var clients = from client in xDocument.Descendants("Client")
select client.Element("ClientName").Value;
3
The following example uses the SelectSingleNode method to select the first book node in which the author's last name meets the specified criteria. The bookstore.xml file (which is provided at the end of this topic) is used as the input file.
<?xml version='1.0'?>
<bookstore xmlns="urn:newbooks-schema">
<book genre="novel" style="hardcover">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>19.95</price>
</book>
<book genre="novel" style="other">
<title>The Poisonwood Bible</title>
<author>
<first-name>Barbara</first-name>
<last-name>Kingsolver</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
// Load the document and set the root element.
XmlDocument doc = new XmlDocument();
doc.Load("newbooks.xml");
// Create an XmlNamespaceManager to resolve the default namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema");
// Select the first book written by an author whose last name is Atwood.
XmlNode book;
XmlElement root = doc.DocumentElement;
book = root.SelectSingleNode("descendant::bk:book[bk:author/bk:last-name='Atwood']", nsmgr);
Console.WriteLine(book.OuterXml);
}
}
4
The input file please references 3
The next example uses the SelectNodes method to select all the book nodes in which the price is greater than a specified amount. The price for each book in the selected list is then programmatically reduced by ten percent. Finally, the updated file is written to the console. The bookstore.xml file (which is provided at the end of this topic) is used as the input file.
XmlDocument doc = new XmlDocument();
doc.Load("bookstore.xml");
XmlNode root = doc.DocumentElement;
// Add the namespace.
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("bk", "urn:newbooks-schema");
// Select all nodes where the book price is greater than 10.00.
XmlNodeList nodeList = root.SelectNodes(
"descendant::bk:book[bk:price>10.00]", nsmgr);
foreach (XmlNode book in nodeList)
{
// Discount prices by 10%.
double price;
price = Math.Round(Convert.ToSingle(
book.LastChild.InnerText) * 0.9, 2);
book.LastChild.InnerText = price.ToString();
}
// Display the updated document.
doc.Save(Console.Out);