2016-03-21 11 views
0

ich lesen haben die folgende XML-Dateikann nicht XML aufgrund übergeordneten Knoten

<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> 

ich den folgenden Code verwende diese Datei zu lesen

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"); 
} 

Hier habe ich den Inhalt von <taSopLoAuto> lesen möchten . Aber ich kann den Dateiinhalt nicht lesen. Liegt das daran, dass die ersten Zeilen im Dokument geschrieben sind? Bitte helfen Sie mir, das Problem zu lösen.

+1

'taSopLotAuto_Items' ist nicht das Top-Level-Element in Ihrem XML und' taSopLotAutoka' gar nicht existiert. Schreibe deinen XPath neu. – Tomalak

+0

Überprüfen Sie diese http://stackoverflow.com/questions/30279306/read-from-xml-files-with-for-without-a-namespace-using-xmldocument –

Antwort

0

Der Namespace ist ein Problem. Sie können Linq verwenden, wie unten

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

Danke. Angenommen, ich habe mehrere Datensätze unter demselben Stammnamen, wie kann ich sie mit einer Schleife durchlaufen. – Babar

+0

Welches Tag wird für mehrere Datensätze wiederholt? Ich weiß nicht, was Sie mit einer Aufzeichnung meinen. – jdweng

+0

Ihr ist nur ein Tag ** ** und sein Kind kommt Einsen in Datei. Wenn dieses Tag wiederholt wird, wie kann ich es tun. – Babar

Verwandte Themen