【问题标题】:Unable to read XML due to parent node由于父节点而无法读取 XML
【发布时间】:2016-03-21 16:13:15
【问题描述】:

我有以下 XML 文件

<eConnect xmlns:dt="urn:schemas-microsoft-com:datatypes">
  <SOPTransactionType>
    <eConnectProcessInfo>
      <ConnectionString>Data Source=DGLSQL1;Initial Catalog=dgl;Persist Security Info=False;Integrated Security=SSPI</ConnectionString>
      <EConnectProcsRunFirst>True</EConnectProcsRunFirst>
    </eConnectProcessInfo>
    <taSopLotAuto_Items>
      <taSopLotAuto>
        <SOPTYPE>2</SOPTYPE>
        <SOPNUMBE>435462</SOPNUMBE>
        <LNITMSEQ>16384</LNITMSEQ>
        <ITEMNMBR>7740</ITEMNMBR>
        <LOCNCODE>18</LOCNCODE>
        <QUANTITY>65</QUANTITY>
        <LOTNUMBR>15483D0104X68X</LOTNUMBR>
      </taSopLotAuto>
    </taSopLotAuto_Items>
  </SOPTransactionType>
</eConnect>

我正在使用下面的代码来读取这个文件

XmlDocument doc = new XmlDocument();
doc.Load("C:\\SOP.XML");
XmlNodeList nodes = doc.SelectNodes("/taSopLotAuto_Items/taSopLotAutoka");
foreach (XmlNode node in nodes)
{      
    string text = node["SOPTYPE"].InnerText; 
    Console.WriteLine(text + "\n");
}

这里想看&lt;taSopLoAuto&gt;的内容。但我无法读取文件内容。这是因为文档中写了前几行吗?请帮我解决问题。

【问题讨论】:

标签: c# xml


【解决方案1】:

命名空间是个问题。你可以像下面这样使用 Linq

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<eConnect xmlns:dt=\"urn:schemas-microsoft-com:datatypes\">" +
                  "<SOPTransactionType>" +
                    "<eConnectProcessInfo>" +
                      "<ConnectionString>Data Source=DGLSQL1;Initial Catalog=dgl;Persist Security Info=False;Integrated Security=SSPI</ConnectionString>" +
                      "<EConnectProcsRunFirst>True</EConnectProcsRunFirst>" +
                    "</eConnectProcessInfo>" +
                    "<taSopLotAuto_Items>" +
                      "<taSopLotAuto>" +
                        "<SOPTYPE>2</SOPTYPE>" +
                        "<SOPNUMBE>435462</SOPNUMBE>" +
                        "<LNITMSEQ>16384</LNITMSEQ>" +
                        "<ITEMNMBR>7740</ITEMNMBR>" +
                        "<LOCNCODE>18</LOCNCODE>" +
                        "<QUANTITY>65</QUANTITY>" +
                        "<LOTNUMBR>15483D0104X68X</LOTNUMBR>" +
                      "</taSopLotAuto>" +
                      "<taSopLotAuto>" +
                        "<SOPTYPE>2</SOPTYPE>" +
                        "<SOPNUMBE>435462</SOPNUMBE>" +
                        "<LNITMSEQ>16384</LNITMSEQ>" +
                        "<ITEMNMBR>7740</ITEMNMBR>" +
                        "<LOCNCODE>18</LOCNCODE>" +
                        "<QUANTITY>65</QUANTITY>" +
                        "<LOTNUMBR>15483D0104X68X</LOTNUMBR>" +
                      "</taSopLotAuto>" +
                      "<taSopLotAuto>" +
                        "<SOPTYPE>2</SOPTYPE>" +
                        "<SOPNUMBE>435462</SOPNUMBE>" +
                        "<LNITMSEQ>16384</LNITMSEQ>" +
                        "<ITEMNMBR>7740</ITEMNMBR>" +
                        "<LOCNCODE>18</LOCNCODE>" +
                        "<QUANTITY>65</QUANTITY>" +
                        "<LOTNUMBR>15483D0104X68X</LOTNUMBR>" +
                      "</taSopLotAuto>" +
                    "</taSopLotAuto_Items>" +
                  "</SOPTransactionType>" +
                "</eConnect>";

            XDocument doc = XDocument.Parse(xml);
            List<XElement> taSopLotAutos = doc.Descendants()
                .Where(x => x.Name.LocalName == "taSopLotAuto")
                .ToList();

            var results = taSopLotAutos.Select(x => new
            {
                sopType = (int)x.Element("SOPTYPE"),
                sopNumbe = (int)x.Element("SOPNUMBE"),
                lnitmsseg = (int)x.Element("LNITMSEQ"),
                locncode = (int)x.Element("LOCNCODE"),
                quantity = (int)x.Element("QUANTITY"),
                lotnumbr = (string)x.Element("LOTNUMBR")
            }).ToList();
        }
    }
}

【讨论】:

  • 谢谢。假设我在同一个根名称下有多个记录,如何使用循环对其进行迭代。
  • 多条记录重复哪个标签?我不知道你说的记录是什么意思。
  • 它们只是一个标签 ,它的子标签出现在文件中。如果这个标签重复了,我该怎么办。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多