I'm trying to create an xml file through c# and I ran into a snag. I'm not sure why, my current code is adding an attribute of xmlns to each of the outer nodes. I only need it on the root node called Warehousereceipt.I tried to remove the namespace, but without it c# won't accept this file as a xml because it doesn't have the schema reference.
XNamespace xNamespace = "urlhere";
XElement newXML = new XElement(xNamespace "WarehouseReceipt",
new XAttribute("Type", "WH"),
new XElement("Number", tempN[0]),
new XElement("ShipperName", tempS[0]),
new XElement("ConsigneeName", tempC[0]),
new XElement("Items",
from str in csv
let fields = str.Split(',')
select new XElement("Item",
new XAttribute("Type", "WI"),
new XElement("Satus", fields[3]),
new XElement("Pieces", fields[6]),
new XElement("Description", fields[5]),
new XElement("PackageName", fields[4]),
new XElement("Length",
new XAttribute("Unit", "in"), fields[7]),
new XElement("Volume",
new XAttribute("Unit", "ft3"), fields[11]),
new XElement("Height",
new XAttribute("Unit", "in"), fields[8]),
new XElement("Width",
new XAttribute("Unit", "in"), fields[9]),
new XElement("Weight",
new XAttribute("Unit", "lb"), fields[10]),
new XElement("WarehouseReceiptNumber", fields[0])
)
),
new XElement("MeasurementUnits",
new XElement("LengthUnit", "in"),
new XElement("VolumeUnit", "ft3"),
new XElement("WeightUnit", "lb")
)
);
return newXML;XElement newXML = new XElement(xNamespace "WarehouseReceipt",
new XAttribute("Type", "WH"),
new XElement("Number", tempN[0]),
new XElement("ShipperName", tempS[0]),
new XElement("ConsigneeName", tempC[0]),
new XElement("Items",
from str in csv
let fields = str.Split(',')
select new XElement("Item",
new XAttribute("Type", "WI"),
new XElement("Satus", fields[3]),
new XElement("Pieces", fields[6]),
new XElement("Description", fields[5]),
new XElement("PackageName", fields[4]),
new XElement("Length",
new XAttribute("Unit", "in"), fields[7]),
new XElement("Volume",
new XAttribute("Unit", "ft3"), fields[11]),
new XElement("Height",
new XAttribute("Unit", "in"), fields[8]),
new XElement("Width",
new XAttribute("Unit", "in"), fields[9]),
new XElement("Weight",
new XAttribute("Unit", "lb"), fields[10]),
new XElement("WarehouseReceiptNumber", fields[0])
)
),
new XElement("MeasurementUnits",
new XElement("LengthUnit", "in"),
new XElement("VolumeUnit", "ft3"),
new XElement("WeightUnit", "lb")
)
);
return newXML;
here is the current output so you can see what i mean. PS: the way the xml file is organized is exactly how i need it, so please don't suggest changing things around the structure.
<WarehouseReceipt Type="WH" xmlns="urlhere">
<Number xmlns="">"3519"</Number>
<ShipperName xmlns=""> "4 NET NETWORKING CORP"</ShipperName>
<ConsigneeName xmlns=""> "ACUAMAR"</ConsigneeName>
<Items xmlns="">
<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"> "5.00"</Height>
<Width Unit="in"> "4.00"</Width>
<Weight Unit="lb"> "10.00"</Weight>
<WarehouseReceiptNumber>"3519"</WarehouseReceiptNumber>
</Item>
<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"> "45.00"</Height>
<Width Unit="in"> "15.00"</Width>
<Weight Unit="lb"> "400.00"</Weight>
<WarehouseReceiptNumber>"3519"</WarehouseReceiptNumber>
</Item>
</Items>
<MeasurementUnits xmlns="">
<LengthUnit>in</LengthUnit>
<VolumeUnit>ft3</VolumeUnit>
<WeightUnit>lb</WeightUnit>
</MeasurementUnits>
</WarehouseReceipt>
CodePudding user response:
See docs: How to control namespace prefixes.
XNamespace xNamespace = "urlhere";
XElement newXML = new XElement(xNamespace "WarehouseReceipt",
new XAttribute(XNamespace.Xmlns "prefix", "urlhere"),
new XAttribute("Type", "WH"),
new XElement("Number", 1),
new XElement("ShipperName", 2),
new XElement("ConsigneeName", 3),
//...
);
Result:
<prefix:WarehouseReceipt xmlns:prefix="urlhere" Type="WH">
<Number>1</Number>
<ShipperName>2</ShipperName>
<ConsigneeName>3</ConsigneeName>
...
</prefix:WarehouseReceipt>