简体   繁体   中英

Should I generate XML as a string in C#?

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?

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

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

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.

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.

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.

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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