1

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?

3
  • what is that you want to achieve? can you clearly state your output ? Commented Jul 3, 2013 at 11:06
  • 1
    I want the Root object converted to an XML string Commented Jul 3, 2013 at 11:08
  • see my below answer.. is that what you want? Commented Jul 3, 2013 at 11:15

1 Answer 1

3

Check out Xml Serializable Generic Dictionary http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx You can do nearly the same for your case.

Upd Then you can serialize yor class using standart serializartion like this

    public static string SerializeObjectToXml<T>(T obj)
    {
        MemoryStream memoryStream = new MemoryStream();
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

        xmlSerializer.Serialize(xmlTextWriter, obj);
        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

        string xmlString = ByteArrayToStringUtf8(memoryStream.ToArray());

        xmlTextWriter.Close();
        memoryStream.Close();
        memoryStream.Dispose();

        return xmlString;
    }

upd2

    public static string ByteArrayToStringUtf8(byte[] value)
    {
        UTF8Encoding encoding = new UTF8Encoding();
        return encoding.GetString(value);
    }

upd3

A more cleare way:

[Serializable]
public class Root
{
    [XmlElement("ITEMS")]
    public BOMItem[] Row { get; set; }
}

[Serializable]
public class BOMItem
{
    [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; }
}

so you can get your xml like this:

        Root r1 = new Root();
        r1.Row = new BOMItem[2];
        r1.Row[0] = new BOMItem {ITEMNO = "1", PARTSOURCE = "11", QTY = "111", USED = "1111"};
        r1.Row[1] = new BOMItem { ITEMNO = "2", PARTSOURCE = "22", QTY = "222", USED = "2222" };
        MessageBox.Show(String.Format("Serialization result: {0}", SerializeObjectToXml(r1)));
Sign up to request clarification or add additional context in comments.

4 Comments

How does that help? I don't see how it demonstrates how to serialize a custom class.
Implemet ISerializable interface in your class and you'll be fine
Where do you get/define ByteArrayToStringUtf8?
Update 3 looks more nicely if you are not going to use something extraordinary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.