简体   繁体   English

名称不能以''字符开头,十六进制值0x20

[英]Name cannot begin with the ' ' character, hexadecimal value 0x20

I have gone through a lot of answers for this but was not able to solve issue so asking. 为此,我经历了很多回答,但无法解决问题,因此提出了要求。

I am getting my xml in a string. 我在字符串中获取我的xml。 It consist of "< 6" as content in some node values. 它由“ <6”作为某些节点值中的内容。

As a result I am getting an exception 结果我得到一个例外

Name cannot begin with the ' ' character, hexadecimal value 0x20. 名称不能以''字符(十六进制值0x20)开头。 Line 3270, position 54. 3270行,位置54。

Here is the code: 这是代码:

string patternToReplaceAnd = "&(?![a-z#]+;)";
Regex reg = new Regex(patternToReplaceAnd);
xml = reg.Replace(xml, "&amp;");
XDocument xDoc = XDocument.Parse(xml);

Can anyone help me out? 谁能帮我吗?

You say you're getting your XML in a string. 您说要以字符串形式获取XML。 You're not. 你不是。 You're getting garbage in a string. 您正在字符串中获取垃圾。

If the garbage is really important to you then you can try and convert it to XML. 如果垃圾对您确实很重要,则可以尝试将其转换为XML。 How you do that depends on just how bad it is, which we can't really judge. 您如何做取决于它的严重程度,我们无法真正判断。

Much better: refuse to accept shoddy goods. 更好的是:拒绝接受伪劣商品。 Go back to the supplier and tell them to generate real XML. 回到供应商,并告诉他们生成真实的XML。

I do realize that this question is old but I came across the same problem today and I hope my answer will help someone who may land on this question in the future. 我确实意识到这个问题很旧,但是今天我遇到了同样的问题,希望我的回答对将来可能会遇到此问题的人有所帮助。

The problem is the content that includes < followed by the space . 问题是包含<后跟space You will have to replace that content with &lt; 您将不得不用&lt;替换该内容&lt; so that It is not recognised as a malformed xml start tag . 因此它不会被识别为格式错误的xml start tag

xml = xml.Replace('< ',"&lt; "); //make sure you include the space after < to avoid replacing actual tags.
XDocument xDoc = XDocument.Parse(xml);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 深入研究XML(Windows Phone)世界,我不理解(名称中不能包含&#39;&#39;字符,十六进制值0x20。) - Delving into the world of XML (Windows Phone) Error I dont understand (The ' ' character, hexadecimal value 0x20, cannot be included in a name.) DataContractSerializer-名称不能以“。”开头 字符,十六进制值0x2E - DataContractSerializer - Name cannot begin with the '.' character, hexadecimal value 0x2E 名称不能以 '&lt;' 字符十六进制值 0x3c 开头 - Name cannot begin with the '<' character hexadecimal value 0x3c 获取System.Xml.XmlException:名称不能以&#39;&#39;字符开头,十六进制值为0x20。 第42行,位置36 - Getting System.Xml.XmlException: Name cannot begin with the ' ' character, hexadecimal value 0x20. Line 42, position 36 XDocument错误名称不能以&#39;&lt;&#39;字符开头,十六进制值0x3C - XDocument Error Name cannot begin with the '<' character, hexadecimal value 0x3C 使用xslt时,名称不能以&#39;=&#39;字符十六进制值0x3d开头 - Name cannot begin with the '=' character hexadecimal value 0x3d when use xslt xml.Linq:“名称不能以 &#39;?&#39; 开头字符,十六进制值 0x3F&quot; - xml.Linq: "Name cannot begin with the '?' character, hexadecimal value 0x3F" 名称不能以“1”字符开头,十六进制值0x31。 第2行,第2位 - Name cannot begin with the '1' character, hexadecimal value 0x31. Line 2, position 2 将Firebase广告添加到Xamarin.Forms会导致:名称不能以&#39;$&#39;字符开头,十六进制值为0x24 - Adding Firebase Ads to Xamarin.Forms causes: Name cannot begin with the '$' character, hexadecimal value 0x24 名称不能以“1”字符开头,十六进制值0x31。 从xml文件中读取时 - Name cannot begin with the '1' character, hexadecimal value 0x31. while reading from an xml file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM