how do you access a node in a node list that only occurs sometimes c# xml




<OrderFeed>
<Order id="1">
<BillingInformation>
<Name>Bruce Ganek</Name>
<Address>99 Main Street</Address>
<City>Cranston</City>
<State>RI</State>
<ZipCode>02910</ZipCode>
</BillingInformation>
<ShippingInformation>
<Name>Governor Chafee</Name>
<Address>82 Smith St # 115</Address>
<City>Providence</City>
<State>RI</State>
<ZipCode>02903-1121</ZipCode>
</ShippingInformation>
<Items>
<Item>
<PartNo>JETSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>10.50</UnitPrice>
<Quantity>2</Quantity>
<TotalCost>21.00</TotalCost>
<CustomerOptions>
<Size>M</Size>
<Color>Green</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>7.50</UnitPrice>
<Quantity>3</Quantity>
<TotalCost>22.50</TotalCost>

<CustomerOptions>
<Size>S</Size>
<Color>White</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSFLASHLIGHT</PartNo>
<Description>N.Y. Jets Flashlight</Description>
<UnitPrice>5.00</UnitPrice>
<Quantity>1</Quantity>
<TotalCost>5.00</TotalCost>

<CustomerOptions/>

</Item>

</Items>
</Order>
<Order id="2">
<BillingInformation>
<Name>Walt Disney</Name>
<Address>DisneyWorld Hotel</Address>
<City>Orlando</City>
<State>FL</State>
<ZipCode>32801</ZipCode>
</BillingInformation>
<ShippingInformation>
<Name>Walt Disney</Name>
<Address>DisneyWorld Hotel</Address>
<City>Orlando</City>
<State>FL</State>
<ZipCode>32801</ZipCode>
</ShippingInformation>
<Items>
<Item>
<PartNo>JETSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>10.50</UnitPrice>
<Quantity>2</Quantity>
<TotalCost>21.00</TotalCost>
<CustomerOptions>
<Size>M</Size>
<Color>Green</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>7.50</UnitPrice>
<Quantity>3</Quantity>
<TotalCost>22.50</TotalCost>

<CustomerOptions>
<Size>S</Size>
<Color>White</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSFLAG</PartNo>
<Description>N.Y. Jets Flag for display</Description>
<UnitPrice>5.00</UnitPrice>
<Quantity>1</Quantity>
<TotalCost>5.00</TotalCost>

<CustomerOptions/>

</Item>

</Items>
</Order>
<Order id="3">
<BillingInformation>
<Name>Tom Brady</Name>
<Address>One Patriot Place</Address>
<City>Foxboro</City>
<State>MA</State>
<ZipCode>02035</ZipCode>
</BillingInformation>
<ShippingInformation>
<Name>Tom Brady</Name>
<Address>2121 George Halas Drive</Address>
<City>Canton</City>
<State>OH</State>
<ZipCode>44708</ZipCode>
</ShippingInformation>
<Items>
<Item>
<PartNo>JETPANTS</PartNo>
<Description>N.Y. Jets Sweatpants</Description>
<UnitPrice>10.50</UnitPrice>
<Quantity>3</Quantity>
<TotalCost>31.50</TotalCost>
<CustomerOptions>
<Size>M</Size>
<Color>Green</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>7.50</UnitPrice>
<Quantity>1</Quantity>
<TotalCost>7.50</TotalCost>

<CustomerOptions>
<Size>S</Size>
<Color>White</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSFLAG</PartNo>
<Description>N.Y. Jets Flag for display</Description>
<UnitPrice>5.00</UnitPrice>
<Quantity>1</Quantity>
<TotalCost>5.00</TotalCost>

<CustomerOptions/>

</Item>

</Items>
</Order>
</OrderFeed>


I'm trying yo access the customer Options within an asp.net web services application. I can't figure out how to go through the Item node set using an XPath expression to get all Items including the Customer Options nodeset. I want the PartNo, Description, UnitPrice,Quantity, TotalCost and then customeroptions if there is any for an item. The tricky part is I get an exception because obviously I end up eventually try storing data that isn't listed for customer options in to a struct I made. This is what I have in my function for web services.



[WebMethod]
public lab3 GetItemListForOrder(int OrderID)
{

string strFileName = Server.MapPath("~\\XML_OrderInfo_Lab3.xml");
XPathDocument xDoc = new XPathDocument(strFileName);
//XPathNodeIterator NodeIter;
XPathNavigator nav;
XPathNodeIterator NodeIter;

nav = xDoc.CreateNavigator();
string searchstring = "//OrderFeed/Order[ " + OrderID + "]/Items//Item";

NodeIter = nav.Select(searchstring);
StringBuilder sb = new StringBuilder();

while (NodeIter.MoveNext())
{
lab3 lab = new lab3();
XPathNavigator Items = NodeIter.Current;
lab.PartNO = Items.SelectSingleNode("PartNo").ToString();
lab.Description = Items.SelectSingleNode("Description").ToString();
lab.UnitPrice = Items.SelectSingleNode("UnitPrice").ToString();
lab.TotalCost = Items.SelectSingleNode("TotalCost").ToString();

lab.Size = Items.SelectSingleNode("Size").ToString();
sb.Append(Items.SelectSingleNode("Color").ToString() + Environment.NewLine);
return lab;
}
}


between the lab. TotalCost line and lab.Size, should be some kind of check to see if there is a customeroption available. I just can't figure it out and how to make it display correctly afterwards.


No comments:

Post a Comment