我有以下XML文件
select t.id, t.data1,
first_value(data2) over (partition by grp order by id) as newcolumn
from (select t.*,
sum(case when data1 = 'value1' then 1 else 0 end) over (order by id) as grp
from t
) t;
我使用以下代码来阅读此文件
<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");
}
的内容。但我无法读取文件内容。这是因为文档中写的前几行吗?请帮我解决问题。
答案 0 :(得分:0)
命名空间是一个问题。您可以使用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();
}
}
}