C#写入特定XML节点

时间:2009-11-04 02:37:09

标签: c# xml

我有一个xml数据库,格式如下:

<Students>
 <Student ID= *GUID NUMBER*>
  <FullName>John Smith</FullName>
  <Address>Blah blah blah</Address>
  and so on...
 <Student ID= *GUID NUMBER*>
  <FullName>Joe Blow</FullName>
  <Address>Blah Blah</Address>
  and so on...

我有一个组合框,可以从这个xml数据中选择在其下拉列表中显示FullName。现在我需要做的是根据在组合框中选择的FullName更新并向所选学生添加节点,再次按下另一个按钮 - 按下“提交”。

1 个答案:

答案 0 :(得分:4)

要选择特定的Student节点,您可以执行以下操作:

XmlDocument xml = new XmlDocument();
xml.LoadXml("<Students>...."); // or xml.Load("yourfile.xml");
XmlElement student = xml.SelectSingleNode(
    String.Format("//Student[@ID='{0}']",
                  yourcombo.SelectedItem.Value)) as XmlElement;
if(student != null)
{
    XmlElement another = xml.CreateElement("another");
    another.InnerText = "Value";
    student.AppendChild(another);

    // do other stuff
}
相关问题