【问题标题】:Error Xpath (System.ArgumentException: 'Noncompliant characters in the path.')错误 Xpath(System.ArgumentException:'路径中的不符合字符。')
【发布时间】:2018-01-08 10:39:39
【问题描述】:

当我尝试从数据库中对 XML 数据进行 Xpath 处理时,出现此错误(从法语 Visual Studio 错误翻译): System.ArgumentException: '路径中有不符合规定的字符。' Error from Visual Studio (French)

这是我的代码:

public partial class WebForm1 : System.Web.UI.Page
{
    SqlConnection connection = new SqlConnection(@"Data Source=SA2426\SQLEXPRESS;Initial Catalog=Proteor;Persist Security Info=True;User ID=sa;Password= root");
    string query = "SELECT Content From Template WHERE Id = '0108f7ac-7853-4543-8ec4-a04cb755ed46' AND Deleted = '0'";

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            SqlCommand cmd = new SqlCommand(query, connection);
            connection.Open();

            SqlDataReader sdr = cmd.ExecuteReader();

            XmlDocument doc = new XmlDocument();

            if(sdr.HasRows)
            {
                while(sdr.Read())
                {
                    string content = sdr["Content"].ToString();
                    System.Diagnostics.Debug.WriteLine(System.Web.HttpUtility.HtmlDecode(content));

                    // Test Decode
                    string contenu = System.Web.HttpUtility.HtmlDecode(content);

                    doc.Load(System.Web.HttpUtility.HtmlDecode(content));

                    XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
                    ns.AddNamespace("msbld", "http://schemas.microsoft.com/developer/msbuild/2003");
                    XmlNode FormId;                        
                    FormId = doc.SelectSingleNode("descendant::Properties/Property[attribute::Name='FormId']", ns);
                    System.Diagnostics.Debug.WriteLine(FormId.OuterXml);
                }
            }                
        }
        finally
        {
            connection.Close();
        }            
    }
}

我必须解码我的 XML,因为此数据中包含特殊字符,如“&lt”、“&gt”... 我不知道这怎么可能...我将“测试解码”复制/粘贴到一个简单的 xml 文档中,以便检查 xml 代码,没关系:

<Root>
  <StorageObject Name="OI-GENOUILLERE ODRA-v2" Id="0108f7ac-7853-4543-8ec4-a04cb755ed46" Type="Adviser.Proteor.Library.TemplateImpl">
    <Properties>
      <Property Name="TimeCreated" Value="27/07/2017 11:14:18" /><Property Name="Enabled" Value="True" /><Property Name="TimePublished" Value="01/01/0001 00:00:00" /><Property Name="TimeLastModified" Value="10/11/2017 09:09:29" /><Property Name="Description" Value="" /><Property Name="ObjectContent" Value=""<Template>
        <Component Type="Adviser.Proteor.Library.TemplateImpl">
          <ExtendedProperty Text="OI" Key="Classification" />
          <ExtendedProperty Text="35176af2-b664-42d6-a7ba-5d06602b0500" Key="ClassificationId" />
          <ExtendedProperty Text="GAO" Key="Activity" />
          <ExtendedProperty Text="58d942ea-8cf7-4089-879c-f8633925473b" Key="ActivityId" />
          <ExtendedProperty Text="OI 36" Key="AnatomicalLevel" />
          <ExtendedProperty Text="76418da5-6830-4a49-aed0-b472c11c67c6" Key="AnatomicalLevelId" />
          <ExtendedProperty Text="ORTHESE INF." Key="Type" />
          <ExtendedProperty Text="13b33a27-57ce-4009-8812-00f8578982fd" Key="TypeId" />
          <Version Version.Major="0" Version.Minor="1" />
          <Properties>
            <Property Name="TimeCreated" Value="27/07/2017 11:14:18" />
            <Property Name="Enabled" Value="True" />
            <Property Name="TimePublished" Value="01/01/0001 00:00:00" />
            <Property Name="TimeLastModified" Value="10/11/2017 09:09:29" />
            <Property Name="Description" Value="" />
          </Properties>
          <Component Type="Adviser.Proteor.Library.StepImpl">
            <Properties>
              <Property Name="FormId" Value="d442dce3-fc1f-49da-92c5-b25d89d76881" />
              <Property Name="ValidationMode" Value="None" />
            </Properties>
            <Component Type="Adviser.Proteor.Library.Impl.Action.Task.OnPageLoadTrigger">
              <Properties>
                <Property Name="PageNumber" Value="1" />
              </Properties>
              <Component Type="Adviser.Proteor.Library.Impl.Action.Task.SetControlValueTask">
                <Properties>
                  <Property Name="Text" Value="{agence}" />
                  <Property Name="ObjectId" Value="dc20a4e2-46d6-47fe-8c75-bf4d7ff012ba" />
                </Properties>
              </Component>

不是完整的代码,但是可以看到代码写的正确。

需要帮助!!!

编辑:目前我刚刚看到了一些东西。有一个 xml 代码封装在一个主要的 xml 代码中:

ObjectContent 的值未“完成”并以其他标记模板开头

【问题讨论】:

    标签: c# asp.net xml visual-studio xpath


    【解决方案1】:

    对于XmlDocument.Load,根据the docs,此重载:

    从指定的 URL 加载 XML 文档。

    您传递给它的不是 URL,而是包含 XML 内容的 string。这就是错误消息指出您的路径中有无效字符的原因 - 您的 XML 根本不是路径。

    您想使用XmlDocument.LoadXml 方法。

    【讨论】:

    • 感谢您的回答。我更改了 XmlDocument 方法,但出现了一个新错误:'
    • 这意味着你在一个属性中有&lt;,例如&lt;element attr="&lt;" /&gt;,这是非法的。应该是&lt;element attr="&amp;lt;" /&gt;。您需要提供minimal reproducible example,但如果我不得不猜测,那么您需要删除HtmlDecode
    猜你喜欢
    • 1970-01-01
    • 2010-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多