tags:

views:

56

answers:

4

I'm generating a large amount of XML documents from a set of values in an Excel file. The only thing that changes for each XML document is the values. I figured the best way to generate these documents was to make a "XML skeleton" (since the XML format never changes) and then plug in symbols like "&%blahNameblahTest", so then I could just preform a Regex.Replace on each value.

I will be handing over this project to another developer and I'm wondering if I should convert the project to generate the XML file manually everytime through the System.XML namespace.

The only advantages I see to this is ensuring that the XML document is valid.

The current method would be more readable than that method, and way faster since I'm generating around 1500 documents.

+1  A: 

I would stick with your existing method.

However, I would add a reference to System.Linq.XML though and do an XElement.Parse() on the output to ensure your resultant document parses correctly. (The one advantage you mentioned to the System.Xml route!)

Kindness,

Dan

Daniel Elliott
A: 
TheMachineCharmer
You're going to want to use a string literal for that multiline string: `string.Format(@"etc...`
Callum Rogers
Thanks Callum Rogers!
TheMachineCharmer
A: 

You could have the best of both worlds by parsing the skelleton XML into a XDocument and perform the replaces in form of a LINQ to XML query.

Maybe write an extension method along the lines of

public static void Replace(this XDocument haystack, String needle, String replacement)
{
    var query = haystack.Root
                        .DescendantsAndSelf()
                        .Where(xe => !xe.HasElements && xe.Value == needle);
    foreach (XElement item in query)
    {
        item.Value = replacement;
    }
}
Jens
A: 

The only advantages I see to this is ensuring that the XML document is valid.

The current method would be more readable than that method, and way faster since I'm generating around 1500 documents.

It seems to me that in designing a process that generates 1500 XML documents, guaranteeing that all of the output is in fact well-formed XML is not a trivial advantage.

Assuming that the typical element containing one of the symbols that you want to replace looks something like this:

<element>$symbol</element>

you could deal with replacements this way:

XmlDocument skeleton = new XmlDocument();
skeleton.Load(inputPath);
foreach (XmlElement elm in skeleton.SelectNodes("//*[starts-with(., '$')]"))
{
   elm.InnerText = GetValue(elm.InnerText);
}
skeleton.Save(outputPath);

You could do a similar thing with XmlReader and XmlWriter, which will produce code that's faster, though not nearly as compact.

An additional advantage of this approach over using Regex.Replace: It only makes two passes through the skeleton XML, once to parse it and once to search it. If you use a regular expression, you'll search the skeleton XML from start to end once for every value you replace.

Robert Rossney