如何使用c#.net在特定位置的xml文件中添加xml节点

时间:2016-07-17 03:20:03

标签: xml c#-4.0

我有一个包含以下详细信息的xml文件。想要添加另一个节点
在第3位。请分享代码以帮助我。

要添加的节点:

<Field id="">
                    <Caption></Caption>
                    <Value></Value>
                    <ElementFinder>
                      <Locator>ID</Locator>
                      <Properties>username</Properties>
                    </ElementFinder>
                  </Field>

原始XML:

<ItemCollection>
              <Field id="a609022b-8604-43a9-b196-b639378eba36">
                <Caption>Enter User name</Caption>
                <Value>test</Value>
                <ActionKeyword>Input</ActionKeyword>
                <ElementFinder>
                  <Locator>ID</Locator>
                  <Properties>username</Properties>
                </ElementFinder>
              </Field>
              <Field id="5932ad89-7be3-4ddf-add1-4e3dcf2609d8">
                <Caption>Enter Password</Caption>
                <Value>Test</Value>
                <ActionKeyword>Input</ActionKeyword>
                <ElementFinder>
                  <Locator>ID</Locator>
                  <Properties>password</Properties>
                </ElementFinder>
              </Field>
              <Action id="f199a3bf-b7e3-42ae-ac02-821bdd3b076b">
                <Caption>Click on Login</Caption>
                <ActionKeyword>Click</ActionKeyword>
                <ElementFinder>
                  <Locator>LinkText</Locator>
                  <Properties>Login</Properties>
                </ElementFinder>
              </Action>                  
            </ItemCollection>

1 个答案:

答案 0 :(得分:1)

假设你知道如何解析XML并使用LINQ-to-XML创建一个新元素,那么你应该发布相同的代码,以便我们可以根据它做出答案:

var raw = "string containing the XML in question";
var doc = XDocument.Parse(raw);

var newFieldRaw = @"<Field id="""">
    <Caption></Caption>
    <Value></Value>
    <ElementFinder>
      <Locator>ID</Locator>
      <Properties>username</Properties>
    </ElementFinder>
</Field>";
var newField = XElement.Parse(newFieldRaw);

然后,为了回答实际问题,您可以找到最后一个<Field>元素,并使用AddAfterSelf()插入新元素:

var lastField = doc.Descendants("Field").Last();
lastField.AddAfterSelf(newField);
Console.WriteLine(doc.ToString());

输出

<ItemCollection>
  <Field id="a609022b-8604-43a9-b196-b639378eba36">
    <Caption>Enter User name</Caption>
    <Value>test</Value>
    <ActionKeyword>Input</ActionKeyword>
    <ElementFinder>
      <Locator>ID</Locator>
      <Properties>username</Properties>
    </ElementFinder>
  </Field>
  <Field id="5932ad89-7be3-4ddf-add1-4e3dcf2609d8">
    <Caption>Enter Password</Caption>
    <Value>Test</Value>
    <ActionKeyword>Input</ActionKeyword>
    <ElementFinder>
      <Locator>ID</Locator>
      <Properties>password</Properties>
    </ElementFinder>
  </Field>
  <Field id="">
    <Caption></Caption>
    <Value></Value>
    <ElementFinder>
      <Locator>ID</Locator>
      <Properties>username</Properties>
    </ElementFinder>
  </Field>
  <Action id="f199a3bf-b7e3-42ae-ac02-821bdd3b076b">
    <Caption>Click on Login</Caption>
    <ActionKeyword>Click</ActionKeyword>
    <ElementFinder>
      <Locator>LinkText</Locator>
      <Properties>Login</Properties>
    </ElementFinder>
  </Action>
</ItemCollection>