我试图从Node提取属性。我写了逻辑,如果属性为空,它将返回null。但是现在我想检查属性是否存在。如果存在,则读取该值,否则返回元素值为null。
<Request deploymentMode="production">
<OrderRequest>
<OrderRequestHeader orderDate="2019-02-12T12:00:00+11:00" orderID="4502057884" orderType="regular" orderVersion="1" type="new">
<Name xml:lang="en">Order by 12</Name>
</Contact>
</OrderRequestHeader>
<ItemOut lineNumber="10" quantity="10.0">
<ItemID>
<SupplierPartID>164375</SupplierPartID>
<BuyerPartID>18992972</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015332095"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">83.79</Money>
</UnitPrice>
<Description xml:lang="en">Nobbys Mixed Nuts 500g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>164375</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015332095</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<Comments xml:lang="en">vendor needs to supply us with min shelf life of 91 days for the stock required</Comments>
<ScheduleLine lineNumber="1" quantity="10.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="20" quantity="9.0">
<ItemID>
<SupplierPartID>1057555</SupplierPartID>
<BuyerPartID>19000465</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015241557"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">40.36</Money>
</UnitPrice>
<Description xml:lang="en">Red Rock Deli Sweet Potato Sea Salt 135g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>1057555</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015241557</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="3.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
<ScheduleLine lineNumber="2" quantity="2.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
<ScheduleLine lineNumber="3" quantity="2.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
<ScheduleLine lineNumber="4" quantity="2.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="30" quantity="4.0" requestedDeliveryDate="2019-02-12T12:00:00+11:00">
<ItemID>
<SupplierPartID>1057537</SupplierPartID>
<BuyerPartID>19000467</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015241533"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">40.36</Money>
</UnitPrice>
<Description xml:lang="en">Red Rock Deli Swt Ptt Grlic & Rsmry 135g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>1057537</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015241533</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="4.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="40" quantity="8.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<ItemID>
<SupplierPartID>1064935</SupplierPartID>
<BuyerPartID>19024974</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015242745"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">40.36</Money>
</UnitPrice>
<Description xml:lang="en">Rd Rock Deli Spcl Chmpng Vigte&Shlt 150g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>320017457</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015242745</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="8.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="50" quantity="1.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<ItemID>
<SupplierPartID>1125962</SupplierPartID>
<BuyerPartID>25032114</BuyerPartID>
<IdReference domain="EAN-13" identifier="29310015243886"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">34.49</Money>
</UnitPrice>
<Description xml:lang="en">Burger Rings 220g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>1125962</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>29310015243886</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="1.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="60" quantity="4.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<ItemID>
<SupplierPartID>421348</SupplierPartID>
<BuyerPartID>25041245</BuyerPartID>
<IdReference domain="EAN-13" identifier="29310015233696"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">20.86</Money>
</UnitPrice>
<Description xml:lang="en">Red Rock Chips H/Soy Chckn 45g Ctn18</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>320003402</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>29310015233696</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="4.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="70" quantity="2.0">
<ItemID>
<SupplierPartID>1108239</SupplierPartID>
<BuyerPartID>25062916</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015245081"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">43.89</Money>
</UnitPrice>
<Description xml:lang="en">Grainwve Plus Swtpot Crmchse & Hrb 180g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Winc</Classification>
<ManufacturerPartID>1108239</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015245081</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="1.0" requestedDeliveryDate="2019-02-20T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
<ScheduleLine lineNumber="2" quantity="1.0" requestedDeliveryDate="2019-02-20T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="80" quantity="1.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<ItemID>
<SupplierPartID>1108318</SupplierPartID>
<BuyerPartID>25062923</BuyerPartID>
<IdReference domain="EAN-13" identifier="19319636001281"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">31.0</Money>
</UnitPrice>
<Description xml:lang="en">Sakata Ricecrisp Swtchilli & Sourcrm 75g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Winc</Classification>
<ManufacturerPartID>1108318</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19319636001281</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="1.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="90" quantity="1.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<ItemID>
<SupplierPartID>320018946</SupplierPartID>
<BuyerPartID>25085114</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015243803"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">44.23</Money>
</UnitPrice>
<Description xml:lang="en">Doritos Crkrs Texan Bbq 160g</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Winc</Classification>
<ManufacturerPartID>320018946</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015243803</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<ScheduleLine lineNumber="1" quantity="1.0" requestedDeliveryDate="2019-02-22T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="100" quantity="11.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<ItemID>
<SupplierPartID>505239</SupplierPartID>
<BuyerPartID>50002078</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015240628"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">38.65</Money>
</UnitPrice>
<Description xml:lang="en">Red Rock Deli Honey Soy Chicken 165g Pkt</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>320012838</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015240628</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<Comments xml:lang="en">MinLife 42 MaxLife 84</Comments>
<ScheduleLine lineNumber="1" quantity="11.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
<ItemOut lineNumber="110" quantity="10.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<ItemID>
<SupplierPartID>505222</SupplierPartID>
<BuyerPartID>50002079</BuyerPartID>
<IdReference domain="EAN-13" identifier="19310015240611"></IdReference>
</ItemID>
<ItemDetail>
<UnitPrice>
<Money currency="AUD">38.65</Money>
</UnitPrice>
<Description xml:lang="en">Red Rock Deli Chips Sea Salt 165g Pkt</Description>
<UnitOfMeasure>CT</UnitOfMeasure>
<PriceBasisQuantity conversionFactor="1" quantity="1.0">
<UnitOfMeasure>CT</UnitOfMeasure>
</PriceBasisQuantity>
<Classification domain="not available">Stock Lines</Classification>
<ManufacturerPartID>320012837</ManufacturerPartID>
<ManufacturerName xml:lang="en"></ManufacturerName>
<ItemDetailIndustry>
<ItemDetailRetail>
<EANID>19310015240611</EANID>
</ItemDetailRetail>
</ItemDetailIndustry>
</ItemDetail>
<Comments xml:lang="en">MinLife 42 MaxLife 84</Comments>
<ScheduleLine lineNumber="1" quantity="10.0" requestedDeliveryDate="2019-02-15T12:00:00+11:00">
<UnitOfMeasure>CT</UnitOfMeasure>
</ScheduleLine>
</ItemOut>
</OrderRequest>
</Request>
我实施的代码是:
我目前不在使用Linq。
XmlNodeList xnList = xmlDoc.SelectNodes("//Request/OrderRequest/ItemOut");
foreach (XmlNode xn in xnList)
{
if (xn.SelectSingleNode("//Request/OrderRequest/ItemOut") != null)
{
RequestedDeliveryDate = xn.SelectSingleNode("//Request/OrderRequest/ItemOut/@requestedDeliveryDate").InnerText;
}
else
{
LogError("//Request/OrderRequest/ItemOut/requestedDeliveryDate not found");
}
我有一个单独的Logerror方法。
我得到的只是请求 requestedDeliveryDate
的Null异常请建议我是否缺少任何东西。
答案 0 :(得分:1)
首先,第4行的xml需要插入Contact起始标记。
XmlNodeList xnList = xmlDoc.SelectNodes("//Request/OrderRequest/ItemOut");
foreach (XmlNode xn in xnList) {
if (xn.SelectSingleNode("@requestedDeliveryDate") != null)
Console.WriteLine(xn.Attributes["lineNumber"].Value + "\t" + xn.Attributes["requestedDeliveryDate"].Value);
else
Console.WriteLine(xn.Attributes["lineNumber"].Value + "\twithout requestedDeliveryDate");
}