根级的数据无效[重复]

时间:2022-03-20 18:33:57

This question already has an answer here:

这个问题已经有了答案:

I have the following XML document:

我有以下XML文档:

<?xml version="1.0" encoding="UTF-8"?>
<Offices id="0" enabled="false">
  <office />
</Offices>

When I try to access it through C#:

当我试图通过c#访问它时:

XmlDocument doc = new XmlDocument();
doc.LoadXml(HttpContext.Current.Server.MapPath("officeList.xml"));

I get this error:

我得到这个错误:

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

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

What is wrong with this line?

这条线有什么问题?

3 个解决方案

#1


88  

This:

这样的:

doc.LoadXml(HttpContext.Current.Server.MapPath("officeList.xml"));

should be:

应该是:

doc.Load(HttpContext.Current.Server.MapPath("officeList.xml"));

LoadXml() is for loading an XML string, not a file name.

LoadXml()用于加载XML字符串,而不是文件名。

#2


12  

For the record:

备案:

"Data at the root level is invalid" means that you have attempted to parse something that is not an XML document. It doesn't even start to look like an XML document. It usually means just what you found: you're parsing something like the string "C:\inetpub\wwwroot\mysite\officelist.xml".

“根级的数据无效”意味着您尝试解析非XML文档的内容。它甚至看起来都不像XML文档。它通常表示您所发现的:您正在解析类似于“C:\inetpub\wwwroot\mysite\officelist.xml”的字符串。

#3


1  

I found that the example I was using had an xml document specification on the first line. I was using a stylesheet I got at this blog entry and the first line was

我发现我使用的示例在第一行有一个xml文档规范。我使用的是我在这个博客条目中得到的样式表,第一行是

<?xmlversion="1.0"encoding="utf-8"?>

which was causing the error. When I removed that line, so that the stylesheet started with the line

这导致了错误。当我删除这一行时,样式表以这一行开始

<xsl:stylesheet version="1.0" xmlns:DTS="www.microsoft.com/SqlServer/Dts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

my transform worked. By the way, that blog post was the first good, easy-to follow example I have found for trying to get information from the XML definition of an SSIS package, but I did have to modify the paths in the example for my SSIS 2008 packages, so you might too. I also created a version to extract the "flow" from the precedence constraints. My final one looks like this:

我改变工作。顺便说一下,这篇博客文章是我发现的第一个很好的、容易理解的示例,用于尝试从SSIS包的XML定义中获取信息,但是我必须修改SSIS 2008包示例中的路径,所以您也可以这么做。我还创建了一个版本来从优先约束中提取“流”。我的最后一个是这样的:

    <xsl:stylesheet version="1.0" xmlns:DTS="www.microsoft.com/SqlServer/Dts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8" />
    <xsl:template match="/">
    <xsl:text>From,To~</xsl:text>
    <xsl:text>
</xsl:text>
    <xsl:for-each select="//DTS:PrecedenceConstraints/DTS:PrecedenceConstraint">
      <xsl:value-of select="@DTS:From"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="@DTS:To"/>
       <xsl:text>~</xsl:text>
      <xsl:text>
</xsl:text>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

and gave me a CSV with the tilde as my line delimiter. I replaced that with a line feed in my text editor then imported into excel to get a with look at the data flow in the package.

给了我一个CSV,斜线作为行分隔符。我用文本编辑器中的行提要替换了它,然后导入到excel中,查看包中的数据流。

#1


88  

This:

这样的:

doc.LoadXml(HttpContext.Current.Server.MapPath("officeList.xml"));

should be:

应该是:

doc.Load(HttpContext.Current.Server.MapPath("officeList.xml"));

LoadXml() is for loading an XML string, not a file name.

LoadXml()用于加载XML字符串,而不是文件名。

#2


12  

For the record:

备案:

"Data at the root level is invalid" means that you have attempted to parse something that is not an XML document. It doesn't even start to look like an XML document. It usually means just what you found: you're parsing something like the string "C:\inetpub\wwwroot\mysite\officelist.xml".

“根级的数据无效”意味着您尝试解析非XML文档的内容。它甚至看起来都不像XML文档。它通常表示您所发现的:您正在解析类似于“C:\inetpub\wwwroot\mysite\officelist.xml”的字符串。

#3


1  

I found that the example I was using had an xml document specification on the first line. I was using a stylesheet I got at this blog entry and the first line was

我发现我使用的示例在第一行有一个xml文档规范。我使用的是我在这个博客条目中得到的样式表,第一行是

<?xmlversion="1.0"encoding="utf-8"?>

which was causing the error. When I removed that line, so that the stylesheet started with the line

这导致了错误。当我删除这一行时,样式表以这一行开始

<xsl:stylesheet version="1.0" xmlns:DTS="www.microsoft.com/SqlServer/Dts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

my transform worked. By the way, that blog post was the first good, easy-to follow example I have found for trying to get information from the XML definition of an SSIS package, but I did have to modify the paths in the example for my SSIS 2008 packages, so you might too. I also created a version to extract the "flow" from the precedence constraints. My final one looks like this:

我改变工作。顺便说一下,这篇博客文章是我发现的第一个很好的、容易理解的示例,用于尝试从SSIS包的XML定义中获取信息,但是我必须修改SSIS 2008包示例中的路径,所以您也可以这么做。我还创建了一个版本来从优先约束中提取“流”。我的最后一个是这样的:

    <xsl:stylesheet version="1.0" xmlns:DTS="www.microsoft.com/SqlServer/Dts" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" encoding="utf-8" />
    <xsl:template match="/">
    <xsl:text>From,To~</xsl:text>
    <xsl:text>
</xsl:text>
    <xsl:for-each select="//DTS:PrecedenceConstraints/DTS:PrecedenceConstraint">
      <xsl:value-of select="@DTS:From"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="@DTS:To"/>
       <xsl:text>~</xsl:text>
      <xsl:text>
</xsl:text>
    </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

and gave me a CSV with the tilde as my line delimiter. I replaced that with a line feed in my text editor then imported into excel to get a with look at the data flow in the package.

给了我一个CSV,斜线作为行分隔符。我用文本编辑器中的行提要替换了它,然后导入到excel中,查看包中的数据流。