I have an object of the following class:
public class Root
{
[XmlElement]
public BOMItems[] Row { get; set; }
}
public class BOMItems
{
[XmlElement("ITEMNO")]
public string ITEMNO { get; set; }
[XmlElement("USED")]
public string USED { get; set; }
[XmlElement("PARTSOURCE")]
public string PARTSOURCE { get; set; }
[XmlElement("QTY")]
public string QTY { get; set; }
}
I am trying to serialize it to an XDocument with this method:
public XDocument TransformClassToXMLBOM(Root rt)
{
var serializer = new XmlSerializer(typeof(Root));
var sww = new StringWriter();
var settings = new XmlWriterSettings();
settings.ConformanceLevel = ConformanceLevel.Auto;
var writer = XmlWriter.Create(sww, settings);
serializer.Serialize(writer, rt);
var doc = new XDocument(
new XElement("Row",
new XElement("ITEMNO"),
new XElement("USED"),
new XElement("PARTSOURCE"),
new XElement("QTY")));
doc.Save(writer);
return doc;
}
I have even tried with an extra element inserted before new XElement("Row", like this:
var doc = new XDocument(
new XElement("Root",
new XElement("Row",...
I always get the error below on this line doc.Save(writer);:
Token StartDocument in state EndRootElement would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.
At first I thought I might be missing an XElement or have something misspelled, but I can't find any mistakes. I don't know how to look at the values in writer to check the results, so I have no idea how to find a solution on this.
I would like to end with something like this:
<Root>
<Row>
<ITEMNO>1</ITEMNO>
<USED>Y</USED>
<PARTSOURCE>BUY</PARTSOURCE>
<QTY>10</QTY>
</Row>
</Root>
How do I find the cause of the problem? What is the correct way to accomplish my desired results?