[英]XML Nodes Count is zero
I have C# project in .Net 2.0. 我在.Net 2.0中有C#项目。 I am trying to read all Event nodes and stores its TextData property in a list of string.
我试图读取所有Event节点并将其TextData属性存储在字符串列表中。 But following SelectNodes has zero count.
但是,跟随SelectNodes的计数为零。
What is the best way to store all TextData values from all Event nodes, in a List of strings? 在字符串列表中存储所有Event节点的所有TextData值的最佳方法是什么?
Note: Some event nodes will not have TextData. 注意:某些事件节点不具有TextData。
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\\Lijo\1.xml");
XmlNodeList xnList = doc.SelectNodes("/Events/Event");
XML: 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>
You can use XmlNamespaceManager
to add prefix to namespace URI mapping. 您可以使用
XmlNamespaceManager
为名称空间URI映射添加前缀。 Then use the prefix in the XPath to access elments in corresponding namespace. 然后使用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);
UPDATE : 更新:
To get all TextData
in a list of string, you can try this way : 要在字符串列表中获取所有
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);
}
You can read XML
for example: 您可以阅读
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>
Reading Nodes: 阅读节点:
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
Hope this helps you! 希望这对你有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.