日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
C#XML解析方式實(shí)例解析

C# XML解析通過(guò)XPath的方式是如何辦到的呢?具體的操作步驟是什么呢?那么下面我們就向你介紹通過(guò)XPath的方式來(lái)實(shí)現(xiàn)C# XML解析,希望對(duì)你了解C# XML解析有所幫助。

C# XML解析通過(guò)XPath的方式的步驟:

1、需要先加載文檔,然后再讀取想要的節(jié)點(diǎn)值。

◆xml文檔

protected XmlDocument doc = null;

◆xml文檔的根元素(節(jié)點(diǎn))

protected XmlElement root = null;

◆xml文檔的名空間管理器

protected XmlNamespaceManager nsmgr = null;

2、接下來(lái)就是加載文檔了

 
 
 
  1. protected void LoadXmlFile(FileInfo xmlFile)
  2.   {
  3. if (xmlFile == null || !xmlFile.Exists)
  4. {
  5.  throw new FileNotFoundException(
  6. string.Format("要解析的文件不存在{0}。",
  7. xmlFile.FullName));
  8. }
  9. //加載文件
  10. this.doc = new XmlDocument();
  11. doc.Load(xmlFile.FullName);
  12. //準(zhǔn)備讀取文件
  13. root = doc.DocumentElement;
  14. string nameSpace = root.NamespaceURI;
  15. nsmgr = new XmlNamespaceManager(doc.NameTable);
  16. nsmgr.AddNamespace("ns", nameSpace);
  17.   }

◆C# XML解析通過(guò)XPath的方式要注意。

a、這兩行是取得xml文檔的名空間

 
 
 
  1. root = doc.DocumentElement;
  2. string nameSpace = root.NamespaceURI;

b、這兩行是建立xml文檔的名空間管理器

 
 
 
  1. nsmgr = new XmlNamespaceManager(doc.NameTable);
  2. nsmgr.AddNamespace("ns", nameSpace);

如果你的xml文檔有名空間,則這部分的代碼是必不可少的。

3、接下來(lái)就是讀取文檔節(jié)點(diǎn)的值了

這里兩個(gè)傳入?yún)?shù)prefixPath是節(jié)點(diǎn)的上級(jí)節(jié)點(diǎn)路徑,xRelativePath是要讀取的節(jié)點(diǎn)名稱。

另外,變量XmlFileInfo是要加載的xml文件。

 
 
 
  1. protected string GetNodeValue(
  2. string prefixPath, string xRelativePath)
  3.   {
  4. if (doc == null)
  5. {
  6.  LoadXmlFile(XmlFileInfo);
  7. }
  8. string xPath = string.Empty;
  9. if (!string.IsNullOrEmpty(xRelativePath))
  10. {
  11.  if (!string.IsNullOrEmpty(prefixPath))
  12.  {
  13.   xPath = prefixPath + xRelativePath;
  14.  }
  15.  else
  16.  {
  17.   xPath = xRelativePath;
  18.  }
  19. }
  20. xPath = xPath.Replace("/", "/ns:");
  21. XmlNode node = root.SelectSingleNode(xPath, nsmgr);
  22. if (node == null)
  23. {
  24.  return null;
  25. }
  26. return node.InnerXml;
  27.   }

可能有的朋友要問(wèn),為什么要設(shè)置兩個(gè)參數(shù)prefixPath和xRelativePath呢,其實(shí)這個(gè)沒(méi)有多大的關(guān)系,我只是為了自己覺(jué)得方便,你也可以在方法外確定了這個(gè)XPath,在方法中只設(shè)置一個(gè)傳入?yún)?shù),效果是一樣的。

◆注意這一行:

 
 
 
  1. xPath = xPath.Replace("/", "/ns:");

如果你的xml文檔帶名空間,則這行是比不可少的,否則會(huì)出現(xiàn)找不到節(jié)點(diǎn),無(wú)法解析的情況。

關(guān)于XPath的一些問(wèn)題:

對(duì)于這樣一個(gè)xml文檔,要查找第一個(gè)節(jié)點(diǎn)下的學(xué)生的Name時(shí)(ID=01),其XPath應(yīng)該是"/ns:Root/ns:Students/ns:Student[1]/ns:Name"。xml對(duì)于重復(fù)的節(jié)點(diǎn)名稱,是按照順序1,2,3...的方式遍歷的,也就是說(shuō)如果要找第N個(gè)Student節(jié)點(diǎn)的下的節(jié)點(diǎn)之,那么應(yīng)使用Student[N]的標(biāo)識(shí)方式。

 
 
 
  1. ﹤?xml version="1.0" encoding="UTF-8" ?﹥
  2. ﹤Root xmlns="urn:ClassNameSpace"﹥
  3. ﹤Class﹥
  4. ﹤ClassID﹥1234﹤/ClassID﹥
  5. ﹤/Class﹥
  6. ﹤Students﹥
  7. ﹤Student﹥
  8. ﹤ID﹥01﹤/ID﹥﹤Name﹥Name01﹤/Name﹥
  9. ﹤/Student﹥
  10. ﹤Student﹥
  11. ﹤ID﹥02﹤/ID﹥﹤Name﹥Name02﹤/Name﹥
  12. ﹤/Student﹥
  13. ﹤/Students﹥
  14. ﹤/Root﹥

當(dāng)然,這里也可以獲取節(jié)點(diǎn)屬性的值,查找滿足特定值的節(jié)點(diǎn)等等,這些和上面獲取節(jié)點(diǎn)值的過(guò)程是類似的。

C# XML解析通過(guò)XPath的方式的實(shí)現(xiàn)就向你介紹到這里,希望對(duì)你了解和學(xué)習(xí)C# XML解析有所幫助。


網(wǎng)頁(yè)名稱:C#XML解析方式實(shí)例解析
網(wǎng)站地址:http://www.dlmjj.cn/article/dhgjgos.html