tags:

views:

709

answers:

8

When generating XML in C#, Is there a problem with generating it as a string? In the past I've found generating XML programatically very verbose and convoluted. Creating the xml through string concatenation/a string builder seems much easier but, it feels like bad practice.
Should I generate XML as a string?

+10  A: 

The XDocument, XElement and XAttribute classes make xml generation in C# much easier to do. Than using the XmlDocument or XmlWriter.

As an example, to produce this:

<RootElement>
    <ChildElement Attribute1="Hello" Attribute2="World" />
    <ChildElement Attribute1="Foo" Attribute2="Bar" />
</RootElement>

You can do this:

XDocument xDocument = new XDocument(
    new XElement("RootElement",
        new XElement("ChildElement",
            new XAttribute("Attribute1", "Hello"),
            new XAttribute("Attribute2", "World")
        ),
        new XElement("ChildElement",
            new XAttribute("Attribute1", "Foo"),
            new XAttribute("Attribute2", "Bar")
        )
    )
);
Robin Day
Will have to try this. Thanks!
macleojw
@Downvoter: Care to comment why?
Robin Day
+2  A: 

Creating the XML through string concatenation is definitely bad practice. The XmlDomDocument isn't very verbose or convoluted; I've always found it to be pretty easy (and safe) to work with.

MusiGenesis
+2  A: 

No, you shouldn't. The XML libraries in .NET (and other platforms) ensure that you create valid XML, and saves you a lot of time worrying about parsing it back in. Also, if someone else needs to use your XML, and complains it's incorrect, you can rule out the actual XML itself, and will save you a lot of time checking you are concatenating properly.

Kazar
+6  A: 

Have you tryed Linq to Xml? it is not very verbose:

XElement xml = new XElement("contacts",
                    new XElement("contact", 
                        new XAttribute("id", "1"),
                        new XElement("firstName", "first"),
                        new XElement("lastName", "last")
                    ),
                    new XElement("contact", 
                        new XAttribute("id", "2"),
                        new XElement("firstName", "first2"),
                        new XElement("lastName", "last2")
                    )
                );
Console.Write(xml);
Gregoire
+1  A: 

Using an XmlTextWriter, you can drop the results into a StringBuilder and use the resulting string. Also, using the XmlTextWriter, you can place the content into a stream. So, if you're looking for flexibility, use an XmlTextWriter.

If your question is about constructing an xml file by string concatenation, don't do that. Use a StringBuilder if you're going to put an xml document together without any other help.

I would avoid manually creating the xml through "concatenation". You increase the likelyhood of errors in the generated xml. Microsoft's xml creation classes have been tested rather thoroughly. Use that to your advantage.

John Fisher
+3  A: 

I would recomend the following approach in most instances when you are generating xml.

  1. Create an XSD for your XML
  2. Compile the XSD into an object
  3. Populate your object
  4. De-serialize your object into xml

Benifits to doing this over just creating xml using the XMLDocuments 1. It's well defined with an XSD. Other developers know what the xml they are receiving is (or what they are modifying) 2. Easy to change in the future. 3. Far greater less chance of typo bugs in your xml document.

Let me know if you need any code examples or additional assistance in how this would be executed.

Note: We generate our cs files from xsds at build time to ensure someone does not manually modify the generated code.

Dan
You mean serialize into xml, right?
Tamás Szelei
That's a reaally good way to do it in many cases.
Piotr Owsiak
Yes, but he wrote de-serialize.
Tamás Szelei
Yes - I intended serialize but was not paying attention to what I wrote
Dan
+4  A: 

Before you think about generating XML using string concatenation instead of a proper library, please go and read the XML specification. Pay particular attention to niceties such as charsets and character references.

You can do it now. I'll wait.

Now ask yourself - do you really want to have to ensure that your concatenated string is valid according to all those rules and write all the helper functions yourself, or do you want to use a well tested library where all that logic has been encapsulated for you?

Good.

Glad that's sorted.

Greg Beech
I also recommend http://hsivonen.iki.fi/producing-xml/ for a summary of the list of issues when writing XML out…
Dominic Mitchell
+1 for actually giving a reason why doing it by string concatenation is a bad idea
Simon Nickerson
+1  A: 

XmlWriter is not that bad when you're formatting your code correctly:

StringBuilder builder = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(builder))
{
    writer.WriteStartDocument();

    writer.WriteStartElement("root");

    writer.WriteStartElement("Node1");
    writer.WriteAttributeString("att1", "value");
    writer.WriteEndElement();

    writer.WriteStartElement("Node2");
    writer.WriteString("inner text");
    writer.WriteEndElement();

    writer.WriteEndElement();

    writer.WriteEndDocument();
}

You could also add identation in your code to facilitate the comprehension.

using (XmlWriter writer = XmlWriter.Create(builder))
{
    writer.WriteStartDocument();
    {
     writer.WriteStartElement("root");
     {
      writer.WriteStartElement("Node1");
      writer.WriteAttributeString("att1", "value");
      writer.WriteEndElement();

      writer.WriteStartElement("Node2");
      writer.WriteString("inner text");
      writer.WriteEndElement();
     }
     writer.WriteEndElement();
    }
    writer.WriteEndDocument();
}
Francis B.