I have an XML file that I need to parse and get some values from it, I need to know how to iterate for repeating items, The lineItems that are in XML, LineItem can be a different number like it can come with 1 line item or 4. So I need to parse XML and get the values of fields of any that come in.
I have 2 change the XML a bit as I cannot post the real XML here, but yes I am able to get the PurchaseOrderDate, also I only need 2 fields from this XML so no need to have a class match with it
For example, I want to get these 2 items:
<BuyerPartNumber>PL123</BuyerPartNumber>
<ItemStatus>Backorder</ItemStatus>
For all the line items that comes in:
<PurchaseOrderAck>
<Header>
<OrderHeader>
<TradingPartnerId>OP</TradingPartnerId>
<OPIEReferenceID>12354</OPIEReferenceID>
<PurchaseOrderNumber>123</PurchaseOrderNumber>
<PurchaseOrderDate>2021-10-08</PurchaseOrderDate>
<InternalOrderNumber>123654</InternalOrderNumber>
</OrderHeader>
<Address>
<AddressTypeCode>ST</AddressTypeCode>
<AddressName>ALBEMARLE</AddressName>
<Address1>122 test Way</Address1>
<Address2>Suite 104</Address2>
<City>SUFFOLK</City>
<State>VA</State>
<PostalCode>12354</PostalCode>
</Address>
</Header>
<LineItems>
<LineItem>
<OrderLine>
<LineSequenceNumber>0001</LineSequenceNumber>
<BuyerPartNumber>PLPww</BuyerPartNumber>
<VendorPartNumber>PL22</VendorPartNumber>
<PartDescription1>Pro-Flex</PartDescription1>
<OrderQty>1.00</OrderQty>
<OrderQtyUOM>EA</OrderQtyUOM>
<UnitPrice>1073.25</UnitPrice>
<ExtendedLineAmount>1073.25</ExtendedLineAmount>
<ItemStatus>Backorder</ItemStatus>
</OrderLine>
</LineItem>
<LineItem>
<OrderLine>
<LineSequenceNumber>0002</LineSequenceNumber>
<BuyerPartNumber>I-99326</BuyerPartNumber>
<VendorPartNumber>I-4319926</VendorPartNumber>
<PartDescription1>ICEROSS</PartDescription1>
<OrderQty>2.00</OrderQty>
<OrderQtyUOM>EA</OrderQtyUOM>
<UnitPrice>292.410000</UnitPrice>
<ExtendedLineAmount>584.82</ExtendedLineAmount>
<ItemStatus>Released</ItemStatus>
</OrderLine>
</LineItem>
<LineItem>
<OrderLine>
<LineSequenceNumber>0003</LineSequenceNumber>
<BuyerPartNumber>I-C880</BuyerPartNumber>
<VendorPartNumber>I-8820</VendorPartNumber>
<PartDescription1>ICEROSS D</PartDescription1>
<OrderQty>2.00</OrderQty>
<OrderQtyUOM>EA</OrderQtyUOM>
<UnitPrice>277.830000</UnitPrice>
<ExtendedLineAmount>555.66</ExtendedLineAmount>
<ItemStatus>Released</ItemStatus>
</OrderLine>
</LineItem>
</LineItems>
and this is my class that want value in:
public class PurchaseOrderModel
{
public string PurchaseOrderDate { get; set; }
public List<int> BuyerPartNumber{ get; set; }
public List<string> ItemStatus{ get; set; }
}
CodePudding user response:
Here's a little LINQ-to-XML to get you started:
XDocument doc = XDocument.Parse(xml);
var col = doc.Root
.Elements("LineItems")
.Elements("LineItem")
.Elements("OrderLine")
.Select(e => new
{
PurchaseOrderDate = doc.Root.Element("Header").Element("OrderHeader").Element("PurchaseOrderDate").Value,
BuyerPartNumber = e.Element("BuyerPartNumber").Value,
ItemStatus = e.Element("ItemStatus").Value
});
foreach (var item in col)
{
Console.WriteLine(item);
}
CodePudding user response:
You could utilize the XmlDocument's GetElementsByTagName() method like the example below:
using System;
using System.Xml;
public class Sample1
{
public static void Main()
{
//Create the XmlDocument.
XmlDocument doc = new XmlDocument();
doc.Load("books.xml");
//Display all the book titles.
XmlNodeList elemList = doc.GetElementsByTagName("title");
for (int i = 0; i < elemList.Count; i )
{
Console.WriteLine(elemList[i].InnerXml);
}
}
}