XML节点计数为零

时间:2014-04-21 03:16:38

标签: c# xml

我在.Net 2.0中有C#项目。我试图读取所有Event节点并将其TextData属性存储在字符串列表中。但是,跟随SelectNodes的计数为零。

在字符串列表中存储所有Event节点的所有TextData值的最佳方法是什么?

注意:某些事件节点不具有TextData。

XmlDocument doc = new XmlDocument();
doc.Load(@"D:\\Lijo\1.xml");

XmlNodeList xnList = doc.SelectNodes("/Events/Event");

XML:

<?xml version="1.0" encoding="utf-16"?>
<TraceData xmlns="http://tempuri.org/TracePersistence.xsd">
  <Header>
    <TraceProvider name="Microsoft SQL Server" MajorVersion="9" MinorVersion="0" BuildNumber="3282" />
  </Header>
  <Events>
    <Event id="15" name="Audit Logout">
      <Column id="12" name="SPID">57</Column>
      <Column id="14" name="StartTime">2014-04-20T18:00:30.573-04:00</Column>
      <Column id="16" name="Reads">6512</Column>
      <Column id="18" name="CPU">0</Column>
      <Column id="9" name="ClientProcessID">3884</Column>
      <Column id="13" name="Duration">70046000</Column>
      <Column id="17" name="Writes">0</Column>
    </Event>


    <Event id="10" name="RPC:Completed">
      <Column id="12" name="SPID">57</Column>
      <Column id="14" name="StartTime">2014-04-20T18:01:40.637-04:00</Column>
      <Column id="16" name="Reads">76</Column>
      <Column id="18" name="CPU">0</Column>
      <Column id="1" name="TextData">exec uspTranslationsGetTranslationByPageName @PageName='invWLUnSuspendReasonCode.aspx',@LanguageID=1</Column>
      <Column id="9" name="ClientProcessID">3884</Column>
      <Column id="13" name="Duration">226</Column>
      <Column id="17" name="Writes">0</Column>
    </Event>

  </Events>
</TraceData>

2 个答案:

答案 0 :(得分:2)

您可以使用XmlNamespaceManager为名称空间URI映射添加前缀。然后使用XPath中的前缀访问相应名称空间中的元素。

var nsManager = new XmlNamespaceManager(doc.NameTable);
nsManager.AddNamespace("d", "http://tempuri.org/TracePersistence.xsd");

XmlDocument doc = new XmlDocument();
doc.Load(@"D:\\Lijo\1.xml");

XmlNodeList xnList = doc.SelectNodes("//d:Events/d:Event", nsManager);

更新:

要获取字符串列表中的所有TextData,您可以尝试这种方式:

XmlNodeList xnList = doc.SelectNodes("d:TraceData/d:Events/d:Event/d:Column[@name='TextData']", nsManager);
var result = new List<string>();
foreach (XmlNode node in xnList)
{
    result.Add(node.InnerText);
}

答案 1 :(得分:0)

您可以阅读XML例如:

<?xml version="1.0" encoding="UTF-8"?>
<Home>
  <TextEntries>
  <follow>
      <item visible="true" navid="fb" url="https://www.facebook.com/" type="text" hideineditor="false" esbname="facebook  Follow Link" linktype="new" id="200038"><![CDATA[facebook]]></item>
      <item visible="true" navid="twitter" url="https://twitter.com/" type="text" hideineditor="false" esbname="twitter  Follow Link" linktype="new" id="200039"><![CDATA[twitter]]></item>
   </follow>
 </TextEntries>
</Home>

阅读节点

  XmlElement root = null;
  XmlDocument doc = new XmlDocument();
  doc.Load(sourceXmlFilePath);
  root = doc.DocumentElement;
  XmlNode rootnode = root.SelectSingleNode("//Home//TextEntries");
  XmlNode followNode = root.SelectSingleNode("//Home//TextEntries//follow");
  string myAttribute=followNode.ChildNodes[0].Attributes["id"].Value;//this will read the attribute ID of first inner element
 string myFirstInnerNodeValue=followNode.ChildNodes[0].InnerText;//this will read the data of first inner node i.e facebook

希望这能帮到你!