XML 查找节点方法。

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.

//document and set the root element.
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);

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值