[英]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, "&");
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 <
您将不得不用<
替换该内容<
so that It is not recognised as a malformed xml start tag
. 因此它不会被识别为格式错误的xml start tag
。
xml = xml.Replace('< ',"< "); //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.