xml.LoadData - 在根级别的数据无效。 1号线,1位 [英] xml.LoadData - Data at the root level is invalid. Line 1, position 1

查看:328
本文介绍了xml.LoadData - 在根级别的数据无效。 1号线,1位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图解析WiX的安装程序里面的一些XML。该XML是从Web服务器返回的所有错误,我的对象。我使用此代码收到错误的问题的标题:

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:

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 是这样的(如被看见在输出的text.txt

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

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



的text.txt 找出来这样的:

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

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



我需要这个XML解析,所以我可以看到,如果我有任何错误

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

修改

这个问题是不是重复标示。在这个问题问这个问题的人是使用的loadXML 解析XML文件。我解析字符串,这是正确的使用的loadXML

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

推荐答案

这里的问题是,的myString 有这样的标题行。无论是有在第一线或本身导致错误的行开始一些隐藏的性格。我切下的第一行,像这样:

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:

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

这解决了我的问题。

这篇关于xml.LoadData - 在根级别的数据无效。 1号线,1位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆