Martin Zugec
Martin Zugec

Reputation: 165

Replace OuterXml OR generate intended string from InnerXml

I have a UI that uses the DataGridView to display the content of XML files.

If XmlNode contains only InnerText, it's quite simple, however I'm having a problem with nodes that contains childnodes (and not only string).

Simple

<node>value</node>

Displayed as "value" in DataGridViewCell.

Complex

<node>
    <foo>bar</foo>
    <foo2>bar</foo2>
</node> 

The problem is that the InnerXml code is not intended and it's very hard to modify in UI.

I've tried to use XmlTextWriter to "beautify" the string - it works quite well, however requires a XmlNode (includes node, not only childnodes) and I cannot assign it back to InnerXml.

I would like to either see following in the UI:

<foo>bar</foo>
<foo2>bar</foo2>

(this can be assigned to InnerXml afterwards)

Or

<node>
    <foo>bar</foo>
    <foo2>bar</foo2>
</node> 

(and find a way how to replace OuterXml with this string).

Thanks for any ideas, Martin

Upvotes: 1

Views: 1918

Answers (1)

har07
har07

Reputation: 89305

You can load the OuterXml to XElement, then use String.Join() to join all child elements of the root node (in other point-of-view, the InnerXml) separated by line break, for example :

XElement e = e.Parse(something.OuterXml);
var result = string.Join(
                          Environment.NewLine, 
                          e.Elements().Select(o => o.ToString())
                        );

Upvotes: 0

Related Questions