获取特定类型的所有子节点不返回任何内容
本文关键字:子节点 返回 任何内 类型 获取 | 更新日期: 2023-09-27 18:29:54
我正试图从Drainage_String
XML节点获取所有Pit
XML元素。
我的问题:当我从节点中检索所有Pit
元素时,XMLNodeList总是空的。我知道节点确实包含2个Pit
元素,所以它应该包含2个节点元素。
出了什么问题?
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlFilePath);
XmlNodeList xNodes = xdoc.DocumentElement.GetElementsByTagName("string_drainage");
foreach (XmlNode dStr in xNodes) {
XmlNodeList pits = dStr.SelectNodes("pit");
MessageBox.Show("Num: "+pits.Count.ToString(), "Number");
// always outputs "Num: 0"
}
我使用的示例数据:
<string_drainage>
<pit>
<name>MH. </name>
<ip>0</ip>
<ratio>0</ratio>
<x>212908.89268569</x>
<y>612015.26122586</y>
<z>80.62414621</z>
</pit>
</string_drainage>
详细数据:
<?xml version="1.0"?>
<xml12d xmlns="http://www.12d.com/schema/xml12d-10.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="English" version="1.0" date="2013-08-27" time="16:33:14" xsi:schemaLocation="http://www.12d.com/schema/xml12d-10.0 http://www.12d.com/schema/xml12d-10.0/xml12d.xsd">
<meta_data>
<units>
<metric>
<linear>metre</linear>
<area>square metre</area>
<volume>cubic metre</volume>
<temperature>celsius</temperature>
<pressure>millibars</pressure>
<angular>decimal degrees</angular>
<direction>decimal degrees</direction>
</metric>
</units>
<application>
<name>12d Model</name>
<manufacturer>12d Solutions Pty Ltd</manufacturer>
<manufacturer_url>www.12d.com</manufacturer_url>
<application>12d Model 10.0C1j</application>
<application_build>10.1.10.22</application_build>
<application_path>C:'Program Files'12d'12dmodel'10.00'nt.x64'12d.exe</application_path>
<application_date_gmt>24-Jul-2013 02:18:30</application_date_gmt>
<application_date>24-Jul-2013 12:18:30</application_date>
<project_name>mjkhjk</project_name>
<project_guid>{30A05217-706A-41c1-AF53-0D1A0975A5D0}</project_guid>
<project_folder>C:'12djobs'mjkhjk</project_folder>
<client>12d Training - NSW</client>
<dongle>572d471062</dongle>
<environment/>
<env4d>C:'12d'10.00'user'env.4d</env4d>
<user>Sam Zielke-Ryner</user>
<export_file_name>Tttt.xml</export_file_name>
<export_date_gmt>27-Aug-2013 06:33:14</export_date_gmt>
<export_date>27-Aug-2013 16:33:14</export_date>
</application>
</meta_data>
<comments>
<manufacturer>12d Solutions Pty Ltd</manufacturer>
<manufacturer_url>www.12d.com</manufacturer_url>
<application>12d Model 10.0C1j</application>
<application_build>10.1.10.22</application_build>
<application_path>C:'Program Files'12d'12dmodel'10.00'nt.x64'12d.exe</application_path>
<application_date_gmt>24-Jul-2013 02:18:30</application_date_gmt>
<application_date>24-Jul-2013 12:18:30</application_date>
<export_file_name>Tttt.xml</export_file_name>
<export_date_gmt>27-Aug-2013 06:33:14</export_date_gmt>
<export_date>27-Aug-2013 16:33:14</export_date>
</comments>
<string_drainage>
<name/>
<time_created>29-Jul-2013 02:02:03</time_created>
<time_updated>29-Jul-2013 02:02:11</time_updated>
<outfall>null</outfall>
<flow_direction>1</flow_direction>
<use_pit_con_points>false</use_pit_con_points>
<data_3d>
<p>212908.89268569 612015.26122586 0</p>
<p>212715.09268598 612007.24091243 84.20896044</p>
</data_3d>
<pit>
<name>MH. </name>
<type>CONC COVER</type>
<road_name/>
<road_chainage>null</road_chainage>
<diameter>1.1</diameter>
<con_point_mode>Points</con_point_mode>
<floating>true</floating>
<hgl>null</hgl>
<chainage>0</chainage>
<ip>0</ip>
<ratio>0</ratio>
<x>212908.89268569</x>
<y>612015.26122586</y>
<z>80.62414621</z>
</pit>
<pit>
<name>MH. </name>
<type>CONC COVER</type>
<road_name/>
<road_chainage>null</road_chainage>
<diameter>1.1</diameter>
<con_point_mode>Points</con_point_mode>
<floating>true</floating>
<hgl>null</hgl>
<chainage>193.96588699</chainage>
<ip>1</ip>
<ratio>0</ratio>
<x>212715.09268598</x>
<y>612007.24091243</y>
<z>84.20896044</z>
</pit>
<pipe>
<name>A</name>
<type>PVC</type>
<diameter>0.15</diameter>
<nominal_diameter>0.15</nominal_diameter>
<us_level>77.38411559</us_level>
<ds_level>79.32377446</ds_level>
<us_hgl>0</us_hgl>
<ds_hgl>0</ds_hgl>
<flow_velocity>0</flow_velocity>
<flow_volume>0</flow_volume>
<attributes>
<real>
<name>nominal diameter</name>
<value>0.15</value>
</real>
<real>
<name>calculated critical cover chainage</name>
<value>4.31482574</value>
</real>
</attributes>
</pipe>
</string_drainage>
</xml12d>
对SelectNodes的调用需要将默认命名空间添加到其中。
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("x", xdoc.DocumentElement.NamespaceURI);
XmlNodeList pits = dStr.SelectNodes("x:pit");
请参阅此处的帮助
我对xml文件使用的一个技巧是,在我使用的xml文件中始终使用别名名称空间。否则就很难向它们写入xpath引用。
我希望这能帮助你
test.xml是xml 的复制/粘贴
static void Main(string[] args)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(@"c:'test.xml");
XmlNodeList xNodes = xdoc.DocumentElement.SelectNodes("pit");
Console.WriteLine("Num: " + xNodes.Count.ToString());
foreach (XmlNode dStr in xNodes)
{
Console.WriteLine("Name: " + dStr.SelectSingleNode("name").InnerText);
Console.WriteLine("ip: " + dStr.SelectSingleNode("ip").InnerText);
Console.WriteLine("ratio: " + dStr.SelectSingleNode("ratio").InnerText);
Console.WriteLine("X: " + dStr.SelectSingleNode("z").InnerText);
Console.WriteLine("Y: " + dStr.SelectSingleNode("y").InnerText);
Console.WriteLine("X: " + dStr.SelectSingleNode("x").InnerText);
}
Console.Read();
}
也许最好使用Xmldocument 的XmlNode和XmlSingleNode