I have a string like this on C#:
string sxml="<id>xpto</id>"
and I want to save an xml file using XLinq:
XElement cursosXML = new XElement("MenuItem", new XCData(sxml));
cursosXML.save("C:\\xpto.xml")
when I read my xml file this appears:
<MenuItem><![CDATA[<id>xpto</id>]]></MenuItem>
but I do not want <![CDATA[
How can I get this result?
<MenuItem><xpto>3<</MenuItem>
Here's how you should add the string to your element
XElement sxml = XElement.Parse("<id>xpto</id>");
XElement cursosXML = new XElement("MenuItem", sxml);
cursosXML.Save("C:\\xpto.xml");
Below Is an explanation of the troubles you've been having. It's just here for your reference
<id>xpto</id> is an XML Element, but you're adding it to your current element as though it were a literal string. When you do this, the computer doesn't think you're adding a new child element to your main element, so it escapes it(explained later)
The code I posted calls XElement.Parse(string), which will take your seralized xml string, and try to generate a valid XElement from it.
What's happening is that < is an escape sequence for < and > is an escape sequence for >.
the reason why your XML parser escapes < and > is because those characters have special meaning, and including them in your XML change the nature of XML.
It's kinda similar to how \n is an escape sequence for the newline character, and how \\ is an escape sequence for \
Related
I am creating some XML using XElement and I then need to convert that XElement into a string to insert into a file.
The problem is that the XML elements are being converted <example> to <example>. This isn't a huge surprise but I don't want this to happen. I need the string exactly as is with the symbols in tact.
Is there any way of avoiding this please?
This is the creation of my Element and the subsequent cast to string:
XElement markup = new XElement("xref", new XAttribute("xrefid", value), string.Empty);
string xmlMarkUp = markup.ToString(); //converts the XML to > and < WHICH I DON'T WANT
XElement markup = new XElement("xref", new XAttribute("xrefid", value)
my guess : OP want value of attribute tag contains another element ex: 123
answer : there is no solution , cause it will violate XML rule Entity References
https://www.w3schools.com/xml/xml_syntax.asp
must always convert "<",">" to "<"; and ">"; respectively within "".
I'm new to C# and XML and all this things. Well, I'm trying to parse an XML file contains special characters like &, < etc and even the file contains more than 10,000 lines.
I'm using
XDocument xDoc = XDocument.Parse(xmlFile);
but whenever it encounters character like & throws exception.
It can change from & to &, < to <
And because of it contains many lines its almost impossible to convert them all manually.
So, please suggest me the best way to solve the problem. Or if possible then some sample code.
input: xml string (contains special char).
This error occur when your document is incorrect.
Anyway , you can try this
First load your file in a string variable named as response
TextReader stringRead = new StringReader(response);
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(stringRead);
I have an XML Document where it contains data with < character.
<Tunings>
<Notes>Norm <150 mg/dl</Notes>
</Tunings>
The code I am using is:
StreamReader objReader = new StreamReader(strFile);
string strData = objReader.ReadToEnd();
XmlDocument doc = new XmlDocument();
// Here I want to strip those characters from "strData"
doc.LoadXml(strData);
So it gives error:
Name cannot begin with the '1' character, hexadecimal value 0x31.
So is there a way to strip those characters from XML before Load calls.?
If this is only occurring in the <Notes> section, I'd recommend you modify the creation of the XML file to use a CDATA tag to contain the text in Notes, like this:
<Notes><![CDATA[Norm <150 mg/dl]]></Notes>
The CDATA tag tells XML parsers to not parse the characters between the <![CDATA[ and ]]>. This allows you have characters in your XML that would otherwise break the parsing.
You can use the CDATA tag for any situation where you know (or have reasonable expectations) of special characters in that data.
Trying to handle special characters at parsing time (without the CDATA) will be more labor intensive (and frustrating) than simply fixing the creation of the XML in the first place, IMO. Plus, "Norm <150 mg/dl" is not the same thing as "Norm 150 mg/dl", and that distinction might be important for whoever needs that information.
As the comments state, you do not have an XML document. If you know that the only way that these documents deviate from legal XML is as in your example, you could run the file through a regular expression and replace <(?:\d) with &. This will find the < adjacent to a number and properly encode it.
I am reading an XML string with XDocument
XmlReader reader = XmlReader.Create(new StringReader(xmltext));
reader.Read();
XDocument xdoc = XDocument.Load(reader);
Then I grab the content of some tags and put them within tags in a different string.
When I try to Load this string in the same way I did with the first, I get an error "An error occurred while parsing EntityName. Line 1, position 344.".
I think it should be parsed correctly since it has beem parsed before so I guess I am missing something here.
I am reading and copying the content of the first XML with (string)i.Element("field").
I am using .net 4
When I grab the content of the xml that I want to use for building another Xml string I use (string)i.Element("field") and this is converting my Xml into string. My next Xml Parsing does not recognize it as an Element anymore so I solved the problem by not using (string) before I read my element, just i.Element("field") and this works.
It sounds like you've got something like this:
<OriginalDocument>
<Foo>A & B</Foo>
</OriginalDocument>
That A & B represents the text A & B. So when you grab the text from the element, you'll get the string "A & B". If you then use that to build a new element like this:
string foo = "<Foo>" + fooText + "</Foo>";
then you'll end up with invalid XML like this:
<Foo>A & B</Foo>
Basically, you shouldn't be constructing XML in text form. It's not clear what you're really trying to achieve, but you can copy an element from one place to another pretty easily in XElement form; you shouldn't need to build a string and then reparse it.
So after spending hours on this issue:
it turns out that if you have an ampersand symbol ("&") or any other XML escape characters within your xml string, it will always fail will you try read the XML.
TO solve this, replace the special characters with their escaped string format
YourXmlString = YourXmlString.Replace("'", "'").Replace("\"", """).Replace(">", ">").Replace("<", "<").Replace("&", "&");
I'm reading XML data from a varchar column in a SQL db, into a linq to sql XElement belonging to an XDocument.
When I execute the XDocument.Save method, the XML is written to file but includes the escape characters. For example, ">" is changed to ">".
Is there an easy way to prevent this?
First, there seems to be no reason to prevent it. Like kenny mentioned, unless special characters are XML encoded, no parser would be able to parse produced XML (because '<' or '>' characters means a lot for that parser). Second, when your parser decodes XML (e.g. you call XElement.Value), all special characters will be converted back to what they originally were. Finally, if you want to keep the original string (e.g. for purposes other than XML parsing), you can use CDATA, which in case of Linq2XML is represented by XCData class.
EDIT: As Rob pointed out, I might have gotten it wrong. If the point is to save add existing XML to a document, without special characters appear, use the following code:
XDocument document = new XDocument();
var xmlFromDb = "<xml>content</xml>";
using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(xmlFromDb)))
{
using (var reader = XmlReader.Create(stream)) {
reader.MoveToContent();
document.Add(XElement.ReadFrom(reader));
}
}