JCastilloLirio
JCastilloLirio

Reputation: 31

parsing XML with special characters

Has the question above stated, I'm trying to parse a string that has a xml on it. I'm using the classes XDocument and XElement to parse the information that is on the XML into a Dictionary. The code looks something like this:

XDocument xDoc = XDocument.Parse(xmlString); 
Dictionary<string, string> dic = new Dictionary<string,string>();
foreach(XElement x in xDoc.Root.Descendants())
{
    temp.Add(x.Name.ToString(),x.Value.ToString());
}

**xmlString is the string that have the xml content

This usually works fine, except when the xmlString has some kind of special character ('>', '<', '&') on a tag. For example when something like this appears:

<Content> #include <stdio.h> </Content>

I assumed that the code that I implemented doesn't work on this case since C# looks at what he believe is an XML tag has should be seen has plain text. Basically, my question is how can i manage to parse this kind of XML that contain special characters has the one i stated before?

Upvotes: 2

Views: 1528

Answers (1)

Tomas Grosup
Tomas Grosup

Reputation: 6514

If I understand you correctly, you are trying to parse an invalid xml.

If you can, I would recommend you to fix the code which PRODUCES this xml and make it valid.

If you cant, you need to write/use a function, which will convert these special characters to their encoded variants.

Upvotes: 3

Related Questions