xml。LoadData—根级的数据无效。1号线,位置1

时间:2021-12-13 14:35:52

I'm trying to parse some XML inside a WiX installer. The XML would be an object of all my errors returned from a web server. I'm getting the error in the question title with this code:

我试图在WiX安装程序中解析一些XML。XML将是我从web服务器返回的所有错误的一个对象。我得到了问题标题中的错误代码:

XmlDocument xml = new XmlDocument();
try
{
    xml.LoadXml(myString);
}
catch (Exception ex)
{
    System.IO.File.WriteAllText(@"C:\text.txt", myString + "\r\n\r\n" + ex.Message);
    throw ex;
}

myString is this (as seen in the output of text.txt)

myString是这个(在text.txt的输出中可以看到)

<?xml version="1.0" encoding="utf-8"?>
<Errors></Errors>

text.txt comes out looking like this:

文本。txt是这样的

<?xml version="1.0" encoding="utf-8"?>
<Errors></Errors>

Data at the root level is invalid. Line 1, position 1.

I need this XML to parse so I can see if I had any errors.

我需要这个XML进行解析,以便查看是否有任何错误。

Edit

编辑

This question is not a duplicate as marked. In that question the person asking the question was using LoadXml to parse an XML file. I'm parsing a string, which is the correct use of LoadXml

这个问题不是重复的。在这个问题中,问这个问题的人是使用LoadXml来解析XML文件的。我正在解析一个字符串,它是LoadXml的正确用法。

6 个解决方案

#1


74  

The hidden character is probably BOM. The explanation to the problem and the solution can be found here, credits to James Schubert, based on an answer by James Brankin found here.

隐藏的角色很可能是BOM。这个问题的解释和解决方法可以在这里找到,这要归功于詹姆斯·舒伯特(James Schubert),基于詹姆斯·布兰金(James Brankin)在这里找到的答案。

Though the previous answer does remove the hidden character, it also removes the whole first line. The more precise version would be:

虽然前面的答案确实删除了隐藏的字符,但它也删除了整个第一行。更精确的版本是:

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
    xml = xml.Remove(0, _byteOrderMarkUtf8.Length);
}

I encountered this problem when fetching an XSLT file from Azure blob and loading it into an XslCompiledTransform object. On my machine the file looked just fine, but after uploading it as a blob and fetching it back, the BOM character was added.

我在从Azure blob获取XSLT文件并将其加载到XslCompiledTransform对象时遇到了这个问题。在我的机器上,这个文件看起来还不错,但是在将它作为blob上传并取回之后,就添加了BOM字符。

#2


38  

Use Load() method instead, it will solve the problem. See more

用Load()方法代替,可以解决问题。看到更多的

#3


12  

The issue here was that myString had that header line. Either there was some hidden character at the beginning of the first line or the line itself was causing the error. I sliced off the first line like so:

这里的问题是myString有那个标题行。要么是在第一行的开头有一些隐藏的字符,要么是线路本身造成了错误。我把第一行切成这样:

xml.LoadXml(myString.Substring(myString.IndexOf(Environment.NewLine)));

This solved my problem.

这解决了我的问题。

#4


8  

I Think that the problem is about encoding. That's why removing first line(with encoding byte) might solve the problem.

我认为问题在于编码。这就是为什么删除第一行(使用编码字节)可能会解决这个问题。

My solution for Data at the root level is invalid. Line 1, position 1. in XDocument.Parse(xmlString) was replacing it with XDocument.Load( new MemoryStream( xmlContentInBytes ) );

我的根级数据解决方案无效。1号线,位置1。在XDocument. parse (xmlString)中,用XDocument替换了它。加载(新的MemoryStream(xmlContentInBytes));

I've noticed that my xml string looked ok:

我注意到我的xml字符串看起来还不错:

<?xml version="1.0" encoding="utf-8"?>

but in different text editor encoding it looked like this:

但是在不同的文本编辑器编码中是这样的:

?<?xml version="1.0" encoding="utf-8"?>

At the end i did not need the xml string but xml byte[]. If you need to use the string you should look for "invisible" bytes in your string and play with encodings to adjust the xml content for parsing or loading.

最后,我不需要xml字符串,只需要xml字节[]。如果需要使用字符串,应该在字符串中查找“不可见”字节,并使用编码来调整xml内容以进行解析或加载。

Hope it will help

希望它能帮助

#5


1  

Save your file with different encoding:

保存不同编码的文件:

File > Save file as... > Save as UTF-8 without signature.

文件>保存文件为…>保存为UTF-8,没有签名。

In VS 2017 you find encoding as a dropdown next to Save button.

在VS 2017中,你会发现编码是保存按钮旁边的下拉菜单。

#6


0  

If your xml is in a string use the following to remove any byte order mark:

如果您的xml位于字符串中,请使用以下方法删除任何字节顺序标记:

        xml = new Regex("\\<\\?xml.*\\?>").Replace(xml, "");

#1


74  

The hidden character is probably BOM. The explanation to the problem and the solution can be found here, credits to James Schubert, based on an answer by James Brankin found here.

隐藏的角色很可能是BOM。这个问题的解释和解决方法可以在这里找到,这要归功于詹姆斯·舒伯特(James Schubert),基于詹姆斯·布兰金(James Brankin)在这里找到的答案。

Though the previous answer does remove the hidden character, it also removes the whole first line. The more precise version would be:

虽然前面的答案确实删除了隐藏的字符,但它也删除了整个第一行。更精确的版本是:

string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
if (xml.StartsWith(_byteOrderMarkUtf8))
{
    xml = xml.Remove(0, _byteOrderMarkUtf8.Length);
}

I encountered this problem when fetching an XSLT file from Azure blob and loading it into an XslCompiledTransform object. On my machine the file looked just fine, but after uploading it as a blob and fetching it back, the BOM character was added.

我在从Azure blob获取XSLT文件并将其加载到XslCompiledTransform对象时遇到了这个问题。在我的机器上,这个文件看起来还不错,但是在将它作为blob上传并取回之后,就添加了BOM字符。

#2


38  

Use Load() method instead, it will solve the problem. See more

用Load()方法代替,可以解决问题。看到更多的

#3


12  

The issue here was that myString had that header line. Either there was some hidden character at the beginning of the first line or the line itself was causing the error. I sliced off the first line like so:

这里的问题是myString有那个标题行。要么是在第一行的开头有一些隐藏的字符,要么是线路本身造成了错误。我把第一行切成这样:

xml.LoadXml(myString.Substring(myString.IndexOf(Environment.NewLine)));

This solved my problem.

这解决了我的问题。

#4


8  

I Think that the problem is about encoding. That's why removing first line(with encoding byte) might solve the problem.

我认为问题在于编码。这就是为什么删除第一行(使用编码字节)可能会解决这个问题。

My solution for Data at the root level is invalid. Line 1, position 1. in XDocument.Parse(xmlString) was replacing it with XDocument.Load( new MemoryStream( xmlContentInBytes ) );

我的根级数据解决方案无效。1号线,位置1。在XDocument. parse (xmlString)中,用XDocument替换了它。加载(新的MemoryStream(xmlContentInBytes));

I've noticed that my xml string looked ok:

我注意到我的xml字符串看起来还不错:

<?xml version="1.0" encoding="utf-8"?>

but in different text editor encoding it looked like this:

但是在不同的文本编辑器编码中是这样的:

?<?xml version="1.0" encoding="utf-8"?>

At the end i did not need the xml string but xml byte[]. If you need to use the string you should look for "invisible" bytes in your string and play with encodings to adjust the xml content for parsing or loading.

最后,我不需要xml字符串,只需要xml字节[]。如果需要使用字符串,应该在字符串中查找“不可见”字节,并使用编码来调整xml内容以进行解析或加载。

Hope it will help

希望它能帮助

#5


1  

Save your file with different encoding:

保存不同编码的文件:

File > Save file as... > Save as UTF-8 without signature.

文件>保存文件为…>保存为UTF-8,没有签名。

In VS 2017 you find encoding as a dropdown next to Save button.

在VS 2017中,你会发现编码是保存按钮旁边的下拉菜单。

#6


0  

If your xml is in a string use the following to remove any byte order mark:

如果您的xml位于字符串中,请使用以下方法删除任何字节顺序标记:

        xml = new Regex("\\<\\?xml.*\\?>").Replace(xml, "");