1

我有以下类的对象:

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 状态的令牌 StartDocument 将导致无效的 XML 文档。如果要编写 XML 片段,请确保将 ConformanceLevel 设置设置为 ConformanceLevel.Fragment 或 ConformanceLevel.Auto。

起初我以为我可能缺少 XElement 或拼写错误,但我找不到任何错误。我不知道如何查看值writer来检查结果,所以我不知道如何找到解决方案。

我想以这样的方式结束:

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

如何找到问题的原因?实现我想要的结果的正确方法是什么?

4

1 回答 1

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

更新2

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

更新3

更清晰的方式:

[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)));
于 2013-07-03T11:15:31.393 回答