c# - 从动态创建的文本框创建新属性

标签 c# xml xpath

我正在尝试通过在选择列表框项时获取面板内动态创建的文本框的值来为所选节点创建新属性。我确定您对我刚才所说的一无所知,所以我给您一张图片让您有一个想法。

enter image description here

所以问题是在我重新加载 xml 文件后我在节点中看到了这个东西

d3p1:DisplayFormat=""xmlns:d3p1="gh"

这是正在运行的代码

if (addEl.Count != 0)
{
    XmlDocument xDoc = new XmlDocument();
    xDoc.Load(xml);

    XmlNode pnode = xDoc.DocumentElement.SelectSingleNode("//Class[@Name='" + currentClass + "']/Property[@Id=" + pList.SelectedItem + "]");
    for (int i = 0; i < availableProperties.SelectedItems.Count; i++)
    {
        string selecteItem = availableProperties.SelectedItem.ToString();
        Control[] controls = table2.Controls.Find("txt" + selecteItem, true);

        foreach (Control item in controls)
        {
            foreach (string n in addEl)
            {
                    pnode.Attributes.Append(xDoc.CreateAttribute(n, item.Text));
                    xDoc.Save(xml);
                    ok = true;
            }
        }
    }
 }

要求的 xml 结构。对不起,我忘了放这个

<Root>
  <Class Name="ECMInstruction" Style="Top">
    <Entity Id="1" Name="DocumentInformation" />
    <Entity Id="2" Name="CustomerInformation" />
    <Property Id="1" Name="DocumentTitle">
    </Property>
    <Property Id="2" Name="DateCreated">
      <Lists>
        <ListName>ws_Users</ListName>
        <ListName>dfdfdfd</ListName>
      </Lists>
    </Property>
    <Property Id="3" Name="Deadline">
    </Property>
  </Class>
  <Class Name="AlphaCertificationsIndividual" Style="Top">
    <Entity Id="1" Name="DocumentInformation" />
    <Property Id="1" Name="DocumentTitle">
    </Property>
    <Property Id="2" Name="DateCreated">
      <Lists>
        <ListName>ws_Users</ListName>
        <ListName>dfdfdfd</ListName>
      </Lists>
    </Property>
    <Property Id="3" Name="Deadline">
    </Property>
  </Class>
</Root>

最佳答案

而不是这个

pnode.Attributes.Append(xDoc.CreateAttribute(n, item.Text));

尝试这个,使用适当的属性名称和值。

pnode.Attributes.Append(xDoc.CreateAttribute("MyNewAttrib")); 
pnode.Attributes["MyNewAttrib"].Value = "newval";

关于c# - 从动态创建的文本框创建新属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25322656/

相关文章:

c# - 在运行时动态地向 TableLayoutPanel 添加控件

Android - TextView 居中定位

javascript - 在 XPATH 中使用 OR 的两个条件

java - 使用 lambda for 机制在 Selenium 中查找元素

xpath - 选择XPath中的第一个结果

c# - 无法使我的服务调用 ServiceKnownType 帮助程序方法

c# - System.Net.Http.HttpClient 超时似乎被忽略了

xml - 为什么xml中允许多个 '>'

xml - 使用 XSLT 打印计数

CFB 中的 C# AES 加密,其中明文长度等于加密长度