EDN Admin
Well-known member
Im creating my first xml file via linq to xml however Im encountering 2 errors:
1) Need to be able to exclude a set of elements when a certain element is NULL
2) Need one portion of the file to be repeatable (i.e. as if it were being created within a "foreach" loop)
My xml file contains info on customer orders. For issue #1, if the customer entered their phone number info, then there are several xml elements that need to be filled in (phone number, number type, phone extension, etc). If they left their phone
number blank however, then the xml elements should be suppressed.
So the xml should either look like this:
---------------------------------------------------
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; phone<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; phonenumber<span style="color:Blue; >555-555-5555<span style="color:Blue; </<span style="color:#A31515; phonenumber<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; numbertype<span style="color:Blue; >mobile<span style="color:Blue; </<span style="color:#A31515; numbertype<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; phone<span style="color:Blue; >
[/code]
---------------------------------------------
OR.....it should simply look like this if its null (everythings suppressed)
--------------------------------------------
<phone />
-------------------------------------------
For issue #2, if a customer placed multiple items into a single order, I need to list ALL items ordered within a single "order" element like the following (currently, my xml is repeating the order id for each item ordered. So if there were 6 items
ordered, it creates 6 "order" elements, with 6 identical "orderId"s but a different item id) :
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; order<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; orderId<span style="color:Blue; >HDK88210<span style="color:Blue; </<span style="color:#A31515; orderId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; itemId<span style="color:Blue; >54994<span style="color:Blue; </<span style="color:#A31515; itemId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Item Desc1<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; itemId<span style="color:Blue; >32165<span style="color:Blue; </<span style="color:#A31515; itemId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Item Desc2<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; order<span style="color:Blue; >
[/code]
-------------------------------------------------------------------------------------
Heres a sample of the code Im using:
<div style="color:Black;background-color:White; <pre>
xml = <span style="color:Blue; new XElement(<span style="color:#A31515; "Orders",
<span style="color:Blue; from order <span style="color:Blue; in dc.Orders
<span style="color:Blue; join orderDet <span style="color:Blue; in dc.OrderDetails <span style="color:Blue; on order.OrderNumber <span style="color:Blue; equals orderDet.OrderNumber
<span style="color:Blue; orderby order.OrderDate
<span style="color:Blue; select <span style="color:Blue; new XElement(<span style="color:#A31515; "Order",
<span style="color:Blue; new XElement(<span style="color:#A31515; "OrderId", order.OrderNumber)),
<span style="color:Blue; new XElement(<span style="color:#A31515; "Item",
<span style="color:Blue; new XElement(<span style="color:#A31515; "ItemId", order.orderItemId),
<span style="color:Blue; new XElement(<span style="color:#A31515; "Description", order.Description))));
[/code]
Any idea how I can resolve these 2 issues?
Thanks.
View the full article
1) Need to be able to exclude a set of elements when a certain element is NULL
2) Need one portion of the file to be repeatable (i.e. as if it were being created within a "foreach" loop)
My xml file contains info on customer orders. For issue #1, if the customer entered their phone number info, then there are several xml elements that need to be filled in (phone number, number type, phone extension, etc). If they left their phone
number blank however, then the xml elements should be suppressed.
So the xml should either look like this:
---------------------------------------------------
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; phone<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; phonenumber<span style="color:Blue; >555-555-5555<span style="color:Blue; </<span style="color:#A31515; phonenumber<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; numbertype<span style="color:Blue; >mobile<span style="color:Blue; </<span style="color:#A31515; numbertype<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; phone<span style="color:Blue; >
[/code]
---------------------------------------------
OR.....it should simply look like this if its null (everythings suppressed)
--------------------------------------------
<phone />
-------------------------------------------
For issue #2, if a customer placed multiple items into a single order, I need to list ALL items ordered within a single "order" element like the following (currently, my xml is repeating the order id for each item ordered. So if there were 6 items
ordered, it creates 6 "order" elements, with 6 identical "orderId"s but a different item id) :
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <<span style="color:#A31515; order<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; orderId<span style="color:Blue; >HDK88210<span style="color:Blue; </<span style="color:#A31515; orderId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; itemId<span style="color:Blue; >54994<span style="color:Blue; </<span style="color:#A31515; itemId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Item Desc1<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; itemId<span style="color:Blue; >32165<span style="color:Blue; </<span style="color:#A31515; itemId<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; description<span style="color:Blue; >Item Desc2<span style="color:Blue; </<span style="color:#A31515; description<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; item<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; order<span style="color:Blue; >
[/code]
-------------------------------------------------------------------------------------
Heres a sample of the code Im using:
<div style="color:Black;background-color:White; <pre>
xml = <span style="color:Blue; new XElement(<span style="color:#A31515; "Orders",
<span style="color:Blue; from order <span style="color:Blue; in dc.Orders
<span style="color:Blue; join orderDet <span style="color:Blue; in dc.OrderDetails <span style="color:Blue; on order.OrderNumber <span style="color:Blue; equals orderDet.OrderNumber
<span style="color:Blue; orderby order.OrderDate
<span style="color:Blue; select <span style="color:Blue; new XElement(<span style="color:#A31515; "Order",
<span style="color:Blue; new XElement(<span style="color:#A31515; "OrderId", order.OrderNumber)),
<span style="color:Blue; new XElement(<span style="color:#A31515; "Item",
<span style="color:Blue; new XElement(<span style="color:#A31515; "ItemId", order.orderItemId),
<span style="color:Blue; new XElement(<span style="color:#A31515; "Description", order.Description))));
[/code]
Any idea how I can resolve these 2 issues?
Thanks.
View the full article