InnerText XmlNode C# - c#

I am trying to create an XMLDocument in C#. This is the file that I am trying to parse.
<root>
<child/>
some text here
</root>
However, when I try to assign the "some text here" to the element, I run into a problem.
In the beginning I have the XmlNode's
InnerText=""
and the XmlNode's
InnerXml=</child>
By doing
node.InnerText+="some text here";
my
InnerXml="";
I do not understand what am I doing wrong.
Note
If I have the following XML - where the text comes before the child, I have no issues.
<root>
some text here
<child/>
</root>

Rather than trying to manipulate the XML by changing the InnerXxx properties, you should do it by invoking AppendChild.
You'd want to append an XmlText element as a new child.

Related

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
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())
);

XmlElement to string conversion

Is there some simple way to convert XmlElement to string ?
This will get the content of the element if the content is text:
element.Value
This will get the content of the element as XML:
element.InnerXml
This will get the element and its content as XML
element.OuterXml
You can look at the Value or InnerText properties of the element.
However, without further details of exactly what you are looking, I can't help more.
Update:
Seeing as you want the XML of all nodes, using InnerXml or OuterXml should do nicely.
Let's say you have this XmlElement:
<node>
Hello
<effect color="pink">
World
</effect>
</node>
With Console.Write(xmlElement.Inner) you see the inside of your node:
Hello <effect color="pink">World</effect>
With Console.Write(xmlElement.Outer) you get everything:
<node>Hello <effect color="pink">World</effect></node>
With Console.Write(xmlElement.Value) you get nothing, because Value always returns null for an XML element.

Why doesn't XDocument.Parse() parse my XML properly?

I am trying to use XDocument.Parse(string s) to parse some XML that is being returned from a REST based API. After the XML is parsed, it creates a new XDocument, but the document doesn't contain the properly parsed XML nodes. The name of the first node is the correct node name, but the value is the the concatenation of all the text from the XML, regardless of which Element is belongs to. Can anybody help me figure out what is going on?
XML
<sci_reply version="1.0">
<send_message>
<device id="00000000-00000000-00000000-00000000">
<error id="303">
<desc>Invalid target. Device not found.</desc>
</error>
</device>
<error>Invalid SCI request. No valid targets found.</error>
</send_message>
</sci_reply>
Debug View of XDocument Object
That's the expected behavior. The Value of a an XML element is concatenation of values of all its children. If you want to actually access the XML, read something about LINQ to XML or classes in the System.Xml.Linq namespace.
Thats just the debugger being nice.
The root is being displayed with all of its children.

why is XLinq reformatting my XML?

I am using XLinq (XML to Linq) to parse a xml document and one part of the document deals with representing rich-text and uses the xml:space="preserve" attribute to preserve whitespace within the rich-text element.
The issue I'm experiencing is that when I have a element inside the rich-text which only contains a sub-element but no text, XLinq reformats the xml and puts the element on its own line. This, of course, causes additional white space to be created which changes the original content.
Example:
<rich-text xml:space="preserve">
<text-run><br/></text-run>
</rich-text>
results in:
<rich-text xml:space="preserve">
<text-run>
<br/>
</text-run>
</rich-text>
If I add a space or any other text before the <br/> in the original xml like so
<rich-text xml:space="preserve">
<text-run> <br/></text-run>
</rich-text>
the parser doesn't reformat the xml
<rich-text xml:space="preserve">
<text-run> <br/></text-run>
</rich-text>
How can I prevent the xml parser from reformatting my element?
Is this reformatting normal for XML parsing or is this just an unwanted side effect of the XLinq parser?
EDIT:
I am parsing the document like this:
using (var reader = System.Xml.XmlReader.Create(stream))
return XElement.Load(reader);
I am not using any custom XmlReaderSettings or LoadOptions
The problem occurs when I use the .Value property on the text-run XElement to get the text value of the element. Instead of receiving \n which would be the correct output from the original xml, I will receive
\n \n
Note the additional whitespace and line break due to the reformatting! The reformatting can also be observed when inspecting the element in the debugger or calling .ToString().
Have you tried this:
yourXElement.ToString(SaveOptions.DisableFormatting)
This should solve your problem.
btw - you should also do a similar thing on load:
XElement.Parse(sr, LoadOptions.PreserveWhitespace);

why XMLDocument AppendChild() adds "amp" to a xml node string value?

i have a URL string that i want to insert to an XMLDocument - XmlElement Node:
"http://xx.xxx.xxx.xx4:7000/SomeURL/Some/?Locale=asaf&;Portal=how"
when write this code :
XmlElement NodeElement = xmlDoc.CreateElement(nodeToCreate);
NodeElement.InnerText = propInfo.GetValue(requestData,null).ToString();
additionalParamsNode.AppendChild(NodeElement);
The Text in the 'NodeElement.InnerText' looks fine, but when i do the appendChild()
an "&amp" is written inside the XMLElement additionalParamsNode.
its like the XMLElement takes the InnerXML , not the InnerText of the NodeElement.
i dont want to write the string with "amp" to the XML cause the link wont work.
how do i appendChild without changing "&" to "&amp"?
If I'm remembering correctly, &'s are one of the illegal characters in XML. That's why it is turned into &amp (the html equivalent of &).
You should be able to wrap the data in CDATA tags.
<![CDATA[http://xx.xxx.xxx.xx4:7000/SomeURL/Some/?Locale=asaf&;Portal=how]]>
You might have to parse out the CDATA tags when you are done though.

Categories