views:

187

answers:

4

Hi!

I'm working with c# .Net

I have a question,

I'm loading Xml file with XDocument.xDoc.Load(file), but it fails because in my content I also have xml tags:

Example: <root><abc><deg></abc></root>

My problem is that the Load function treats the <deg> as an Xml tag without a matching "</deg>"...

My question is, how can i replace the "<" and ">" of the "deg" with the matching "&lt;" "&gt;" in the easiest way?

N.B. my file is very big and I have a lot of tags...

Thanks!

A: 

If you can get at that section before you load it into the XmlDocument then you could use the HttpUtility.HtmlEncode method to entity-escape the content for you.

The other thing you might want to consider is wrapping your XML-looking content as CDATA, which will effectively hide this content from the parser.

Andrew Hare
hi! HttpUtility.HtmlEncode didn't do anything with the file. about CDADA my problem is how to easely find the problematic tag...
Jack
i thought that i might do it with some kind of regex...
Jack
+2  A: 

What you're trying to do is difficult to do with the standard .NET libraries, unless you want to do a lot of difficult parsing. If there is any rhyme or reason to your non-ended tags, it would help a lot. For example, is there a known list of tags that are not closed? If so, the searching and replacing would not be bad.

But, if it is truly open-ended, if any tag could be unclosed, then you'll need to use something like HTML Tidy. The .Net wrapper of this is can be found here. With this,solution, the <deg> tag would be converted to <deg/>. HTML Tidy wrapper can also fix a few other problems that cause xml to be mal-formed.

Once your file contains well-formed xml, then you can load it into xml objects easily. Then if you have other work to do on the document, you'll at least be able to see it as xml.

Patrick Karcher
nice answer... i have no known list of tags... it can be any tag. i will try to work with the html tidy. after it will be converted to <deg/> (it would be much easier to find them) i will convert it again to <deg> i think that this might work. hope that this library will work for me. (my file is actually in SGML format)
Jack
upvote our answers if they are good! :)
Patrick Karcher
:). i anyway will, but lets wait for some more answers. i want it to be solved!!! and there are a lot of expert like you that could give me some more good answers.
Jack
A: 

If this file is really big then you should use XmlReader instead of XmlDocument and there is no "not closed tags" issue.

http://msdn.microsoft.com/en-us/library/system.xml.xmlreader%28VS.80%29.aspx

Example: http://stackoverflow.com/questions/45481/how-to-do-streaming-read-of-a-large-xml-file-in-c-3-5

dario-g
the file is actually in SGML format.(that why i didn't use xDoc.LoadXml)how can changing from XmlDocument to XmlReader help me?
Jack
Example is on stackoverflow :) I edited my answer. :)
dario-g
thanks! i will try to check it tomorrow morning and let you know :)i'm struggling with it for quite some time. :)
Jack
So, if you read it using an xmlReader, it will recognizes unclosed tags, and then you convert it to xmlDocument or XDocument or whatever, you'll have closed tags?
Patrick Karcher
+1  A: 

Standard regex disclaimer goes here... - sometimes they can come in handy for HTML cleanup scenarios.

Give this approach a try:

string input = "<root><abc><deg><foo></abc><bar></root>";
string pattern = @"(<(?<tag>\w+)>)(?!.*?</\k<tag>>)";
string result = Regex.Replace(input, pattern,
                         match => HttpUtility.HtmlEncode(match.Value));
XDocument document = XDocument.Parse(result);
Console.WriteLine(document.ToString());

Of course be mindful of the file size and that other suggestions maybe more suitable if performance is important for the overall process.

EDIT: the Html Agility Pack is an alternate option for sanitizing any malformed content. If you know the content you could go in there and replace them with valid closing tags.

Ahmad Mageed
hi! thank for the answer... your sample did worked for me. but it didn't work with my file.although i think that your answer is very close, and helping alot. my file is in SGML format. when i try to use your example i get "Data at the root level is invalid. Line 1, position 1." (thats why i didn't use the LoadXml from the beginning...). perhaps a can some how convert the SGML to Xml and then, with your solution, it would solve my problem.
Jack
@Jack did you try using just `Load(filename)` ? Are you loading a string or a file? Your original code showed a filename, whereas I was using a string, so just to be sure we're all on the same page. Also, what does the beginning of your SGML file look like? Perhaps you could update your question with a small sample of the SGML file.
Ahmad Mageed