2014-09-25 93 views
0

我有下面的XML:XML与命名空间的XML的LINQ解析在C#

<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'> 
    <env:Header/> 
    <env:Body> 
     <DigSales xmlns="urn:nike:sale:DigitalSalesToSabrix" xmlns:ns2="http://xmlns.int.nike.com/WSFault" FileTime="2014-09-19T23:16:21Z"> 
      <Tran OrderType="STANDARD" OperatorID="" IsComplimentary="false" ConsumerID="1260046" Currency="USD" InvoiceNo="17779214" BillDate="2014-09-19" BillTime="2014-09-19T22:23:14Z" OrderDate="2014-09-18T20:50:01Z" TranDate="2014-09-19T22:23:14Z" OrderNo="O917950017" StoreNo="150" TranNo="17779214" IsTaxExempt="false"> 
       <Item isTaxExempt="false" IsTaxInvoiceStatus="true" TaxDate="2014-09-18T13:49:56" IsShipSend="true" Style="SX4801" Commodity_CD="531024" Desc="Nike Hyper Elite Crew Basketball Socks" ActValue="36.00" ID="00884498939512" InvSource="BRD" NormValue="36.00" Qty="2" Type="2001" isNotOnFile="false"> 
        <Tax IsManualOverride="false" TaxAmt="3.15"/> 
        <ShipTo Country="US" GeoCode="8382" PostalCode="94102" State="CA" City="SFO" District="" Address2="" Address1="P.O. Box 15687"/> 
        <ShipFrom ShipFromNode="BRD" Country="US" GeoCode="7533" PostalCode="38118" State="TN" City="Memphis" Address2="" Address1="4775 TUGGLE RD DOOR 54"/> 
       </Item> 
       <Item isTaxExempt="false" IsTaxInvoiceStatus="true" TaxDate="2014-09-18T13:49:56" IsShipSend="false" Style="" Commodity_CD="7812" Desc="Shipping" ActValue="8.00" ID="00000000000100" NormValue="8.00" Qty="1" Type="2002" isNotOnFile="false"> 
        <Tax IsManualOverride="false" TaxAmt="0.70"/> 
        <ShipTo Country="US" GeoCode="8382" PostalCode="94102" State="CA" City="SFO" District="" Address2="" Address1="P.O. Box 15687"/> 
        <ShipFrom ShipFromNode="150" Country="US" GeoCode="7533" PostalCode="38118" State="TN" City="Memphis" Address2="" Address1="4775 TUGGLE RD DOOR 54"/> 
       </Item> 
       <ContactInfo LastName="PO Box" FirstName="Test"/> 
      </Tran> 
     </DigSales> 
    </env:Body> 
</env:Envelope> 

我无法解析为 “过渡”。我写了以下解析:

var body = xdoc.Descendants(XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/") + "Body").Single(); 

      var nmspc = XNamespace.Get("http://schemas.xmlsoap.org/soap/envelope/"); 

      var tran = from trans in xdoc.Element(nmspc + "Body").Elements("DigSales") 
         select trans.Elements("DigSales").Elements("Tran").ToList(); 

请帮忙。

+0

你似乎是混乱的“解析”别的东西。你已经有了xdoc,它很可能是一个包含所有解析数据的对象。你在做什么?那么它不适合你? – LVBen 2014-09-25 20:40:00

+0

我需要一个数据表中的Tran数据和另一个数据表中的Item数据以及InvoiceNo作为将出现在这两个表中的外键。 – 2014-09-25 20:43:42

回答

1

这将让你的“过渡”元素的IEnumberable:

var nmspc = XNamespace.Get(@"http://schemas.xmlsoap.org/soap/envelope/"); 
var ns2 = XNamespace.Get(@"urn:nike:sale:DigitalSalesToSabrix"); 

var trans = xdoc.Root.Elements(nmspc + "Body").Elements(ns2 + "DigSales").Elements(ns2 + "Tran"); 
+0

谢谢吨本,它工作:) – 2014-09-25 22:22:48