1

I've been using the XML Formatting solution from Format XML String to Print Friendly XML String, however I'm finding cases that I want to visually format XML documents that are missing elements, or I want to visually format XML 'style' documents.

I'm trying to achieve a solution similar to https://www.webtoolkitonline.com/xml-formatter.html as it seemingly ignores tags, and does formatting only based on hierarchy. I believe their implementation is based on RegEx.

I'm currently using the implementation in the following way:

    private void FormatXML_Click(object sender, RoutedEventArgs e)
    {
        var holdXML = TextEditor.Text;
        TextEditor.Clear();
        try {
            TextEditor.Text = PrintXML(holdXML);}
        catch {
            TextEditor.Text = holdXML;}
    }

    static string PrettyXml(string xml)
    {
        var stringBuilder = new StringBuilder();
        var element = XElement.Parse(xml);
        var settings = new XmlWriterSettings();
        settings.OmitXmlDeclaration = true;
        settings.Indent = true;
        settings.NewLineOnAttributes = true;
        using (var xmlWriter = XmlWriter.Create(stringBuilder, settings))
        { element.Save(xmlWriter); }
        return stringBuilder.ToString();
    }

Ideally this input:

<shipto><name>Ola Nordmann</name><address>Langgt 23</address><city>4000 Stavanger</city><country>Norway</country></shipto><item><title>Empire Burlesque</title><note>Special Edition</note><quantity>1</quantity><price>10.90</price></item><item><title>Hide your heart</title><quantity>1</quantity><price>9.90</price></item>

Would produce this output:

<shipto>
    <name>Ola Nordmann</name>
    <address>Langgt 23</address>
    <city>4000 Stavanger</city>
    <country>Norway</country>
</shipto>
<item>
    <title>Empire Burlesque</title>
    <note>Special Edition</note>
    <quantity>1</quantity>
    <price>10.90</price>
</item>
<item>
    <title>Hide your heart</title>
    <quantity>1</quantity>
    <price>9.90</price>
</item>
Ethan
  • 73
  • 10
  • But that's not valid XML. Surround it with a root node. – Broots Waymb Apr 24 '19 at 20:07
  • @BrootsWaymb Yes, exactly - I'm running into cases where there are missing elements/nodes, and still need to be able to format it into visually readable text. – Ethan Apr 24 '19 at 20:16
  • Is it only the missing root that can occur or other irregularities like unbalanced start/end tags? – sticky bit Apr 24 '19 at 20:42
  • @stickybit No, not just missing root, definitely have seen cases of unbalanced start/end tags. – Ethan Apr 24 '19 at 21:33
  • For the simple case of a missing root element, see [XML Error: There are multiple root elements](https://stackoverflow.com/q/5042902/3744182). But beyond that, `XmlReader` is designed to parse well-formed XML. If you need to parse not-well-formed "XML" in general, you could look at [How to parse invalid (bad / not well-formed) XML?](https://stackoverflow.com/q/44765194). Or if it's valid HTML see [What is the best way to parse html in C#?](https://stackoverflow.com/q/56107). – dbc Apr 25 '19 at 01:26
  • If your string consists of a sequence of well-formed XML fragments, You can use `XmlExtensions.FormatXml(holdXML, conformanceLevel : ConformanceLevel.Fragment)` where `XmlExtensions.FormatXml` comes from [this answer specifically](https://stackoverflow.com/a/68073898/3744182) to [Format XML string to print friendly XML string](https://stackoverflow.com/q/1123718/3744182) to format it. – dbc Jun 21 '21 at 20:45

0 Answers0