【问题标题】:invalid attribute character xml docfrag innerxmll无效的属性字符 xml docfrag innerxmll
【发布时间】:2010-12-11 11:00:48
【问题描述】:

以下几行工作正常,它们将正确的值写入 xml 文件。

问题是试图更改第一个标签。它一直说“

我有什么自动取款机:

<Question type ="">
<QuestionName>test</QuestionName>
</Question type>

<Question type ="">
<QuestionName>test</QuestionName>
</Question type>

但我希望它完全颠倒:(这是试图实现这一点时发生错误的地方)

<QuestionName>
   <Question type =""></Question type>
</QuestionName>  

<QuestionName>
   <Question type =""></Question type>
</QuestionName>  

以下代码有效,但仅适用于第一个示例。

 docFrag.InnerXml = "<Question type=\"" + lblQuestion.SelectedValue + "\">" +
           "<QuestionName>" + txtQuestionName.Text + "</QuestionName>" +
           "</Question>";

【问题讨论】:

    标签: c# asp.net xml


    【解决方案1】:

    我在这里看到的是您正在尝试使用 XmlDocument 类来读取给定的 xml。它不适用于松散的 xml。它需要带有根标记和 xml 声明的正确 xml。要使用松散的 xml,请使用 Linq to Xml。

    【讨论】:

    • 以上代码只是总代码的一部分,我只是想换个顺序。它确实根据我在我的网站上提供的输入写入 xml。
    【解决方案2】:

    你的结束标签应该省略属性 - 它只是

    </Question>
    

    还有;通过连接构建 XML 很容易逃避错误;我会在这里使用 LINQ-to-XMl:

    string s = new XElement("QuestionName",
      new XElement("Question",
        new XAttribute("type", "foo"),
        "bar"
      )
    ).ToString();
    

    【讨论】:

    • 它一直在说构造函数需要更多的争论,不确定如何解决这个问题。以前从未使用过 LINQ。问候
    • @Tonz - 为我“按原样”编译和运行...?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-26
    • 1970-01-01
    • 2022-01-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-27
    相关资源
    最近更新 更多