开发者

Loading XML Document - Name cannot begin with the zero character

开发者 https://www.devze.com 2023-04-11 08:54 出处:网络
I am trying to load something which claims to be an XML document into any type of .net XML object: XElement, XmlDocument, or XmlTextReader.All of them throw an exception :

I am trying to load something which claims to be an XML document into any type of .net XML object: XElement, XmlDocument, or XmlTextReader. All of them throw an exception :

Name cannot begin with the '0' character, hexadecimal value 0x30

The error related to a bit of 'XML'

<chart_value 
    color="ff4400" 
    alpha="100" 
    size="12" 
    position="cursor" 
    decimal_char="." 
    0="" 
/>

I believe the problem is the author should not have named an attribute as 0.

If I could change this I would, but I do not have control of this feed. I suppose those who use it are using more permissive tools. Is there anyway I can load this as XML without throwing an error?

There is no XML declaration e开发者_运维问答ither, nor namespace or contract definition. I was thinking I might have to turn it into a string and do a replace, but this is not very elegant. Was wondering if there was any other options.


As many have said, this is not XML.

Having said that, it's almost XML and WANTS to be XML, so I don't think you should use a regex to screw around inside of it (here's why).

Wherever you're getting the stream, dump into into a string, change 0= to something like zero= and try parsing it.

Don't forget to reverse the operation if you have to return-to-sender.


If you're reading from a file, you can do something like this:

        var txt = File.ReadAllText(@"\path\to\wannabe.xml");
        var clean = txt.Replace("0=", "zero=");
        var doc = new XmlDocument();
        doc.LoadXml(clean);

This is not guaranteed to remove all potential XML problems -- but it should remove the one you have.


Just replace the Numeric value with '_' Example: "0=" replace to "_0=" I hope that will fix the problem, thanks.


It might claim to be an XML document, but the claim is clearly false, so you should reject the document.

The only good way to deal with bad XML is to find out what bit of software is producing it, and either fix it or throw it away. All the benefits of XML go out of the window if people start tolerating stuff that's nearly XML but not quite.


The 0="" obviously uses an invalid attribute name 0. You'd probably have to do a find/replace to try and fix the XML if you cannot fix it at the source that created it. You might be able to use RegEx to try to do more efficient manipulation of the XML string.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号