I'd like to create a simple XMLreader which reads a complete node (including subnodes) as text:
string TXML = @"<xml><text>hallöle</text></xml>";
XmlReader r = XmlReader.Create(new StringReader(TXML));
r.Read(); r.Read();
string o = r.ReadOuterXml();
ReadOuterXml does the job but it unescapes the already escaped signs:
"<text>hallöle</text>"
I whish to have the result:
"<text>hallöle</text>"
How can I ommit that 'unescaping'. I want to store this fragments to a db and do need that escaping. Furthermore I dont want to parse and recreate the fragments.
I had a similar problem, I wanted to keep the escaped characters when reading from xml, but in may case when calling ReadOuterXml(), only some of characters were kept and at least oane was transformed (I had " instead of ")
My solution was the following:
string TXML = @"<xml><text>hallöle</text></xml>";
TXML = TXML.Replace("&", "&");
XmlTextReader r = new XmlTextReader(new StringReader(TXML));
r.Read(); r.Read();
// now we are at the text element
r.ReadStartElement()
var content = SecurityElement.Escape(r.ReadContentAsString())
r.ReadEndElement()
I found two solutions. Both not very nice, but maybe you can tell me which has less drawbacks.
Both solutions rely on direcly using the ´XmlTextReader´ instead of ´XmlReader´. It comes with the property ´LinePosition' which lead me to the first solution and with the method ´ReadChars´ as basis for the second one.
Problems:
Code
string TXML = @"<xml><data></data><rawnode at=""10 4""><text>hallöle</text><z d=""2"">3</z></rawnode><data></data></xml>";
//XmlReader r = XmlReader.Create(new StringReader(TXML));
XmlTextReader r = new XmlTextReader(new StringReader(TXML));
// read to node which shall be retrived "raw"
while ( r.Read() )
{
if ( r.Name.Equals("rawnode") )
break;
}
// here we start
int Begin = r.LinePosition;
r.Skip();
int End = r.LinePosition;
// get it out
string output=TXML.Substring(Begin - 2, End - Begin);
Problems:
Code:
// ... again create XmlTextReader and read to rawnode, then:
// here we start
int buflen = 15;
char[] buf = new char[buflen];
StringBuilder sb= new StringBuilder("<",20);
//get start tag and attributes
string tagname=r.Name;
sb.Append(tagname);
bool hasAttributes = r.MoveToFirstAttribute();
while (hasAttributes)
{
sb.Append(" " + r.Name + @"=""" + r.Value + @"""");
hasAttributes = r.MoveToNextAttribute();
}
sb.Append(@">");
r.MoveToContent();
//get raw inner data
int cnt;
while ((cnt = r.ReadChars(buf, 0, buflen)) > 0)
{
if ( cnt<buflen )
buf[cnt]=(char)0;
sb.Append(buf);
}
//append end tag
sb.Append("</" + tagname + ">");
// get it out
string output = sb.ToString();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With