I am reading the Value of a element <content>Text with <br /> break</content> into a string and showing that string in a TextBox. Afterwards the user can modify it possibly with additional HTML tags. Upon using linq to query the exact node to insert the user-input all spiky bracket <> from html tags are normalized into <br />. So how do I preseve my html tags?
I already read and tried the solutions from these questions but I failed to apply it to my use case: c# XML avoid html encode using XDocument
Get html tags embedded in xml using linq
Keep HTML tags in XML using LINQ to XML
This is an example of my xml file:
<events>
<event id="0">
<content><br/></content>
</event>
</events>
How I load and query:
XDocument xml;
XmlTextReader xtr = new XmlTextReader(this.pathXML);
xtr.Normalization = false;
xml = XDocument.Load(xtr, LoadOptions.PreserveWhitespace);
xtr.Close();
var nodeToEdit = xml.Descendants("event").Where(x => (string)x.Attribute("id") == "0");
How I manipulate my xml file with user input:
string userinput = "Text with <br /> break"; // This is read from TextBox control inside Form
foreach (var item in nodeToEdit.Elements())
{
if(item.Name == "content")
{
item.Value = userinput;
}
}
How I save:
changeSaveIndent(xml, this.pathXML);
public static void changeSaveIndent(XDocument x, string path)
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
// Indent by tab. Can be changed to " " or similar
settings.IndentChars = "\t";
using (XmlWriter writer = XmlTextWriter.Create(path, settings))
{
x.Save(writer);
}
}
My expected xml output file should look like this:
<events>
<event id="0">
<content>Text with <br /> break</content>
</event>
</events>
sorry for the long post..
<events> <event id="0"> <content>Text with <br /> break</content> </event> </events>