2016-03-21 72 views
0

我有下面的XML文件無法讀取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"); 
} 

在這裏,我想讀的<taSopLoAuto>內容。但我無法閱讀文件內容。這是因爲文檔中寫了幾行?請幫我解決問題。

+1

'taSopLotAuto_Items'不是XML中的頂級項目,並且'taSopLotAutoka'甚至不存在。重寫您的XPath。 – Tomalak

+0

看看這個http://stackoverflow.com/questions/30279306/read-from-xml-files-with-or-without-a-namespace-using-xmldocument –

回答

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(); 
     } 
    } 
} 
+0

謝謝。假設我有相同根名下的多個記錄,我怎樣才能使用循環來迭代它們。 – Babar

+0

哪個標籤重複多個記錄?我不知道你的記錄是什麼意思。 – jdweng

+0

他們只有一個tage ** **,它的孩子出現在文件中。如果這個標籤重複,我怎麼能這樣做。 – Babar