1

我正在尝试从XML 节点获取所有PitXML 元素。Drainage_String

我的问题:当我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>
4

3 回答 3

1

我希望这能帮到您

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();
    }
于 2013-09-02T01:45:45.897 回答
1

对 SelectNodes 的调用需要将默认命名空间添加到其中。

XmlNamespaceManager nsmgr = new XmlNamespaceManager(xdoc.NameTable);
nsmgr.AddNamespace("x", xdoc.DocumentElement.NamespaceURI);
XmlNodeList pits = dStr.SelectNodes("x:pit");

请参阅此处的帮助

我对 xml 文件使用的一个技巧是在我使用的 xml 文件中始终使用别名的命名空间。否则很难编写对它们的 xpath 引用。

于 2013-09-02T03:55:06.437 回答
-1

也许更好地使用 XmlDocument 的 XmlNode 和 XmlSingleNode

于 2013-09-02T01:51:05.317 回答