I'm working in c#, and I was needing to convert a .csv to xml. I pretty much got it down, and understand how to do it. However I'm struggling to have it right with how i need it setup.
XNamespace xNamespace = "urlhere";
XElement newXML = new XElement(xNamespace "WarehouseReceipts",
new XAttribute("xmlns", "urlhere"),
from str in csv
let fields = str.Split(',')
select new XElement("WarehouseReceipt",
new XAttribute("Type", "WH"),
new XElement("Number", fields[9]),
new XElement("ShipperName", fields[10]),
new XElement("ConsigneeName", fields[11]),
new XElement("Items",
new XElement("Item",
new XAttribute("Type", "WI"),
new XElement("Satus", fields[0]),
new XElement("Pieces", fields[3]),
new XElement("Description", fields[2]),
new XElement("PackageName", fields[1]),
new XElement("Length",
new XAttribute("Unit", "in"), fields[4]),
new XElement("Volume",
new XAttribute("Unit", "ft3"), fields[8]),
new XElement("Height",
new XAttribute("Unit", "in"), fields[8]),
new XElement("Width",
new XAttribute("Unit", "in"), fields[6]),
new XElement("Weight",
new XAttribute("Unit", "lb"), fields[7]),
new XElement("WarehouseReceiptNumber", fields[9])
)
),
new XElement("MeasurementUnits",
new XElement("LengthUnit", "in"),
new XElement("VolumeUnit", "ft3"),
new XElement("WeightUnit", "lb")
)
)
);
return newXML;
Now, I will show you what the prints out in the console, so you can have an idea of how it comes out in the xml format.
<WarehouseReceipts xmlns="urlhere">
<WarehouseReceipt Type="WH" xmlns="">
<Number>"3519"</Number>
<ShipperName>"4 NET NETWORKING CORP"</ShipperName>
<ConsigneeName>"ACUAMAR"</ConsigneeName>
<Items>
<Item Type="WI">
<Satus>"On Hand"</Satus>
<Pieces>"10"</Pieces>
<Description>"APPLE NEW IPAD"</Description>
<PackageName>"Case"</PackageName>
<Length Unit="in">"5.00"</Length>
<Volume Unit="ft3">"0.60"</Volume>
<Height Unit="in">"0.60"</Height>
<Width Unit="in">"4.00"</Width>
<Weight Unit="lb">"10.00"</Weight>
<WarehouseReceiptNumber>"3519"</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits>
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
<WarehouseReceipt Type="WH" xmlns="">
<Number>"3519"</Number>
<ShipperName>"4 NET NETWORKING CORP"</ShipperName>
<ConsigneeName>"ACUAMAR"</ConsigneeName>
<Items>
<Item Type="WI">
<Satus>"On Hand"</Satus>
<Pieces>"20"</Pieces>
<Description>"APPLE IMAC "</Description>
<PackageName>"Box"</PackageName>
<Length Unit="in">"35.00"</Length>
<Volume Unit="ft3">"273.40"</Volume>
<Height Unit="in">"273.40"</Height>
<Width Unit="in">"15.00"</Width>
<Weight Unit="lb">"400.00"</Weight>
<WarehouseReceiptNumber>"3519"</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits>
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
</WarehouseReceipts>
That whats above, is incorrect, and i need it to come out how it is shown below.
<WarehouseReceipts xmlns="urlhere">
<WarehouseReceipt Type="WH">
<Number>WR-1-22</Number>
<ShipperName>shipper</ShipperName>
<ConsigneeName>consignee</ConsigneeName>
<Items>
<Item Type="WI">
<Status>OnHand</Status>
<Pieces>3</Pieces>
<Description>description2</Description>
<PackageName>Package type2</PackageName>
<WHRItemID>2</WHRItemID>
<Length Unit="in">4.00</Length>
<Height Unit="in">4.00</Height>
<Width Unit="in">4.00</Width>
<Weight Unit="lb">6.00000000000000088818</Weight>
<Volume Unit="ft3">0.11000000000000000056</Volume>
<Model>model2</Model>
<WarehouseReceiptNumber>WR-1-22</WarehouseReceiptNumber>
</Item>
<Item Type="WI">
<Status>OnHand</Status>
<Pieces>4</Pieces>
<Description>description</Description>
<PackageName>Package type</PackageName>
<LocationCode>RCV01</LocationCode>
<Length Unit="in">1.00</Length>
<Height Unit="in">3.00</Height>
<Width Unit="in">2.00</Width>
<Weight Unit="lb">16.00</Weight>
<Volume Unit="ft3">0.01000000000000000021</Volume>
<Model>model</Model>
<WarehouseReceiptNumber>WR-1-22</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits>
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
</WarehouseReceipts>
Any assistance on where i went wrong, would be greatly appreciated.
CodePudding user response:
please disregard.
After toying with it some more, i managed to get it how i need.
Someone made a comment about sorting the csv and that gave me the idea to completely reorder the csv to make it easier to convert to the xml format needed. Thank you,
CodePudding user response:
I used beyond compare to find difference. Not sure what you need help with. See picture
You are doing too much in one statement. See following :
XNamespace xNamespace = "urlhere";
string[] csv = null;
XElement WarehouseReceipt = null;
XElement items = null;
for(int i = 0; i < csv.Length; i )
{
string[] fields = csv[i].Split(new char[] {','});
if(i == 0)
{
XElement newXML = new XElement(xNamespace "WarehouseReceipts",
new XAttribute("xmlns", "urlhere"),
new XElement("WarehouseReceipt",
new XAttribute("Type", "WH"),
new XElement("Number", fields[9]),
new XElement("ShipperName", fields[10]),
new XElement("ConsigneeName", fields[11])
));
WarehouseReceipt = newXML.Descendants("WarehouseReceipt").FirstOrDefault();
items = new XElement("Items");
WarehouseReceipt.Add(items);
}
items.Add(new XElement("Item",
new XAttribute("Type", "WI"),
new XElement("Satus", fields[0]),
new XElement("Pieces", fields[3]),
new XElement("Description", fields[2]),
new XElement("PackageName", fields[1]),
new XElement("Length",
new XAttribute("Unit", "in"), fields[4]),
new XElement("Volume",
new XAttribute("Unit", "ft3"), fields[8]),
new XElement("Height",
new XAttribute("Unit", "in"), fields[8]),
new XElement("Width",
new XAttribute("Unit", "in"), fields[6]),
new XElement("Weight",
new XAttribute("Unit", "lb"), fields[7]),
new XElement("WarehouseReceiptNumber", fields[9])
));
WarehouseReceipt.Add(new XElement("MeasurementUnits",
new XElement("LengthUnit", "in"),
new XElement("VolumeUnit", "ft3"),
new XElement("WeightUnit", "lb")
));
}