1

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 "

    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? how do solve this?

3 Answers 3

2

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<> . 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) )
    ));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your help, I'll try that and get back to you in a couple of minutes
OK, see my Edit about XElement inst of XDocument.
Serializing objects "by hand" is in my opinion a bad idea. Whenever something changes, you have to maintain the serialization code. I would go for XmlSerializier whenever possible. It makes most of the boilder plate code for you, but it's easy to tweak if you need custom features.
@Achim: you're right but I took this to be at the border of serialization and "storing as XML".
1

The ctor of XDocument expects other objects like XElement and XAttribute. Have a look at the documentation. What you are looking for is 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. Should be much faster.

2 Comments

Thank you, I'll try that and get back to you in a couple of minutes
See msdn.microsoft.com/de-de/library/bb299124%28v=vs.90%29.aspx It's an extension method, so you have to add "using System.Xml.XPath" to your code.
0

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

1 Comment

Hi Dona bhatt, welcome. Please consider adding more information.

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.