将类序列化为XML会导致“无效的XML文档”错误

时间:2013-07-03 10:57:37

标签: c# xml-serialization

我有以下类的对象:

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; }
}

我正尝试使用此方法将其序列化为XDocument

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;
}

我甚至尝试在new XElement("Row",之前插入一个额外的元素:

var doc = new XDocument( 
               new XElement("Root",
                    new XElement("Row",...

我总是在这一行doc.Save(writer);上面得到以下错误:

  

状态EndRootElement中的Token StartDocument将导致   XML文档无效。确保ConformanceLevel设置为   如果需要,设置为ConformanceLevel.Fragment或ConformanceLevel.Auto   写一个XML片段。

起初我以为我可能会错过一个XElement或者拼错了,但我找不到任何错误。我不知道如何查看writer中的值来检查结果,所以我不知道如何找到解决方案。

我想结束这样的事情:

<Root>
  <Row>
    <ITEMNO>1</ITEMNO>
    <USED>Y</USED>
    <PARTSOURCE>BUY</PARTSOURCE>
    <QTY>10</QTY>
  </Row>
</Root>

如何找到问题的原因?达到预期结果的正确方法是什么?

1 个答案:

答案 0 :(得分:3)

查看Xml Serializable Generic Dictionary http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx 你可以为你的情况做几乎一样的事。

UPD 然后你可以使用像这样的标准序列化序列化你的类

    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

更清晰的方式:

[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; }
}

所以你可以像这样得到你的xml:

        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)));