0

I trying to extract attributes from a Node. I have written the logic if the attribute is blank it returns null. But now i want to check if the attribute exists or not. If exists read the value otherwise return element value as null.

XML File (image): XML File

<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 &amp; 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&amp;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 &amp; 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 &amp; 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>

Code I implemented is :

I am not using Linq at the moment.

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

I have a seperate method for Logerror.

All I get is Null exception for request requestedDeliveryDate

Please suggest if I am missing something.

srinivas
  • 13
  • 6
  • could you post your xml as text in OP, rather than image – Anu Viswan Feb 27 '19 at 01:23
  • https://stackoverflow.com/questions/7186796/how-do-i-check-particular-attributes-exist-or-not-in-xml – Chetan Feb 27 '19 at 01:32
  • Hi @Chetan Ranpariya, Tried that earlier but somehow it was throwing error with Object reference not set to an instance of an object. – srinivas Feb 27 '19 at 01:47
  • Can you share that code? @srinivas. It would help to understand what's wrong with it and how to correct it. as per your current code You already have list of nodes in `xnList` and you are trying to select Single Node again from each of the node in the list but not checking if `requestedDeliveryDate` attribute exist or not. If you have a code which uses the approach of the other question I shared, please share the code and explain what error you are getting which line of code gives the error. – Chetan Feb 27 '19 at 01:54
  • You might want to update to using `System.Xml.Linq` Dom instead. It's much easier to work with than the older `System.Xml` Dom. – juharr Feb 27 '19 at 02:01
  • @ChetanRanpariya Error Code is as below 'xNode.ParentNode.Attributes["requestedDeliveryDate"].Value' threw an exception of type 'System.NullReferenceException' Data: {System.Collections.ListDictionaryInternal} HResult: -2147467261 HelpLink: null InnerException: null Message: "Object reference not set to an instance of an object." Source: "a98f30d3b0e2484c8a79da5251730268" StackTrace: " at <>x.<>m0(Program <>4__this, XmlDocument xmlDoc)" TargetSite: {System.String <>m0(SaveStaplesXMLOrderDataToDB.Program, System.Xml.XmlDocument)}' – srinivas Feb 27 '19 at 02:10

1 Answers1

1

First of all, the xml at line 4 need to insert Contact start tag.

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");
}
Miller Cy Chan
  • 897
  • 9
  • 19