1

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

Martin Zugec
  • 165
  • 1
  • 7
  • How do you assign xml to DataGridView? Is `node` element root of your xml? – Sergey Berezovskiy Aug 07 '14 at 16:01
  • Hello Sergey, I'm passing the InnerXml (which can be simple value to XML) as a value. Node is not root element, it can contain any value – Martin Zugec Aug 07 '14 at 17:11
  • It's really hard to tell what you are doing and what result you are expecting to get. It would be nice if you would show some sample xml, the way you bind it to grid, and result you have now (possibly screenshot of grid) as well as result you want – Sergey Berezovskiy Aug 07 '14 at 17:26
  • Hello Sergey, there is no binding (unfortunately the result is merge of different documents). Each DataRow represents a single object and each column represents a single value. The value can be either simple string value or can contain child elements. Example XML: ` ABC 1 2 ` In DataGrid, it should display Name as one column (with value ABC) and VHDs as another column (with values 1 2 ) – Martin Zugec Aug 07 '14 at 17:34
  • Sorry, was afk. So, you have DataTable with single column, where you have some xml? – Sergey Berezovskiy Aug 07 '14 at 19:45
  • Not exactly. I have datatable with multiple columns. Columns are generated from an XML file and values can be either string or InnerXml. – Martin Zugec Aug 07 '14 at 21:22

1 Answers1

0

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())
                        );
har07
  • 88,338
  • 12
  • 84
  • 137