I'm getting a string from string GetXmlString()
; this I cant change.
I have to append this to an xml within a new XElement ("parent" , ... );
, to the ...
area.
This string I'm getting is of the following format.
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
...
The final result I want is this to be like
<parent>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
<tag name="" value =""></tag>
...
</parent>
when I just pass the string as XElement("root", GetXmlString())
< and > are encoded to <
and >
When I try XElement.Parse(GetXmlString())
or XDocument.Parse(GetXmlString())
I get the There are multiple root elements exception.
How do I get the required output without escaping the brackets? What am I missing?
The simplest option is probably to give it a root element, then parse it as XML:
If you need to append to an existing element, you can use:
An alternative to Jon's suggestion would be to create an
XmlReader
for your fragment and parse from that:This would be useful if the 'parent' element already exists, else simple concatenation of the XML nodes at each end and parsing would be simpler.