简体   繁体   English

将对象列表序列化为 XDocument

[英]Serializing a list of objects to XDocument

I'm trying to use the following code to serialize a list of objects into XDocument, but I'm getting an error stating that "Non white space characters cannot be added to content "我正在尝试使用以下代码将对象列表序列化到 XDocument 中,但我收到一条错误消息,指出“无法将非空白字符添加到内容中”

    public XDocument GetEngagement(MyApplication application)
    {
        ProxyClient client = new ProxyClient();
        List<Engagement> engs;
        List<Engagement> allEngs = new List<Engagement>();
        foreach (Applicant app in application.Applicants)
        {
            engs = new List<Engagement>();
            engs = client.GetEngagements("myConnString", app.SSN.ToString());
            allEngs.AddRange(engs);
        }

        DataContractSerializer ser = new DataContractSerializer(allEngs.GetType());

        StringBuilder sb = new StringBuilder();
        System.Xml.XmlWriterSettings xws = new System.Xml.XmlWriterSettings();
        xws.OmitXmlDeclaration = true;
        xws.Indent = true;

        using (System.Xml.XmlWriter xw = System.Xml.XmlWriter.Create(sb, xws))
        {
            ser.WriteObject(xw, allEngs);
        }

        return new XDocument(sb.ToString());
    }

What am I doing wrong?我究竟做错了什么? Is it the XDocument constructor that doesn't take a list of objects?是不采用对象列表的XDocument构造函数吗? how do solve this?如何解决这个问题?

I would think that last line should be我认为最后一行应该是

 return XDocument.Parse(sb.ToString());

And it might be an idea to cut out the serializer altogether, it should be easy to directly create an XDoc from the List<> .完全删除序列化程序可能是一个想法,直接从List<>创建 XDoc 应该很容易。 That gives you full control over the outcome.这使您可以完全控制结果。

Roughly:大致:

var xDoc = new XDocument( new XElement("Engagements", 
         from eng in allEngs
         select new XElement ("Engagement", 
           new XAttribute("Name", eng.Name), 
           new XElement("When", eng.When) )
    ));

The ctor of XDocument expects other objects like XElement and XAttribute. XDocument 的构造函数需要其他对象,如 XElement 和 XAttribute。 Have a look at the documentation.查看文档。 What you are looking for is XDocument.Parse(...).您正在寻找的是 XDocument.Parse(...)。

The following should work too (not tested):以下内容也应该有效(未测试):

XDocument doc = new XDocument();
XmlWriter writer = doc.CreateNavigator().AppendChild();

Now you can write directly into the document without using a StringBuilder.现在您可以直接写入文档,而无需使用 StringBuilder。 Should be much faster.应该快得多。

I have done the job this way.我是这样完成工作的。

private void button2_Click(object sender, EventArgs e)
{
    List<BrokerInfo> listOfBroker = new List<BrokerInfo>()
    {
    new BrokerInfo { Section = "TestSec1", Lineitem ="TestLi1" },
    new BrokerInfo { Section = "TestSec2", Lineitem = "TestLi2" },
    new BrokerInfo { Section = "TestSec3", Lineitem ="TestLi3" }
    };

    var xDoc = new XDocument(new XElement("Engagements",
        new XElement("BrokerData",
     from broker in listOfBroker

     select new XElement("BrokerInfo",
       new XElement("Section", broker.Section),
       new XElement("When", broker.Lineitem))
    )));

    xDoc.Save("D:\\BrokerInfo.xml");
}

public class BrokerInfo
{
    public string Section { get; set; }
    public string Lineitem { get; set; }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM