-->

Is there an XElement equivalent to XmlWriter.Write

2019-02-25 03:49发布

问题:

I'm converting some code that currently uses an XmlWriter to create a document to instead return an XElement of the content.

So far, I'm enjoying structuring the code in a way that mimics the structure of the document, but there is content that was written using XmlWriter.WriteRaw to avoid re-xmlizing the xml. I can't find any equivalent in the System.Xml.Linq namespace. Does one exist?

回答1:

XElement.Parse() should do the trick.

For example:

XElement e = new XElement("root",
    new XElement("child",
        XElement.Parse("<big><blob><of><xml></xml></of></blob></big>"),
        new XElement("moreXml")));


回答2:

Caveat: only applicable if your purpose is simply for rendering the XML string, and you're sure the contents are already XML

Since XElement.Parse "re-xmlizes" the already existing XML, you could set the contents to a 'placeholder' value (as suggested here) and replace it in the rendered output:

var d = new XElement(root, XML_PLACEHOLDER);
var s = d.ToString().Replace(XML_PLACEHOLDER, child);

Note that this won't guarantee 'pretty formatting' unless child already has it.

Testing this in LinqPad seems to indicate that 'Replacing' is faster than using Parse:

void Main()
{
    // testing:
    // * https://stackoverflow.com/questions/1414561/how-to-add-an-existing-xml-string-into-a-xelement
    // * https://stackoverflow.com/questions/16586443/adding-xml-string-to-xelement
    // * https://stackoverflow.com/questions/587547/how-to-put-in-text-when-using-xelement
    // * https://stackoverflow.com/questions/5723146/is-there-an-xelement-equivalent-to-xmlwriter-writeraw

    var root = "root";
    var childContents = "<name>Fname</name><age>23</age><sex>None of your business</sex>";
    var child = "<child>" + childContents + "</child>";

    parse(root, child, true);
    replace(root, child, true);

// this fails, per https://stackoverflow.com/questions/16586443/adding-xml-string-to-xelement
try {
        parse(root, childContents, true);
    } catch(Exception ex) {
        ex.Dump();
    }
// this works, but again, you don't get the pretty formatting
    try {
        replace(root, childContents, true);
    } catch(Exception ex) {
        ex.Dump();
    }

    "Xml Parsing".Vs(new [] { "parse", "replace" }
        , n => parse(root, child, false)
        , n => replace(root, child, false)
    );
}

// Define other methods and classes here
void parse(string root, string child, bool print) {
    var d = new XElement(root, XElement.Parse(child));
    var s = d.ToString();
    if(print) s.Dump("Parse Result");
}

const string XML_PLACEHOLDER = "##c##";
void replace(string root, string child, bool print) {
    var d = new XElement(root, XML_PLACEHOLDER);
    var s = d.ToString().Replace(XML_PLACEHOLDER, child);
    if(print) s.Dump("Replace Result");
}

where Vs is a wrapper function for running each delegate 10000 times inside a stopwatch.



标签: .net xelement