2016-07-17 7 views
1

以下の詳細を含むxmlファイルがあります。 3番目の位置に別のノード
を追加します。私を助けるコードを教えてください。c#.netを使用してxmlファイルを特定の場所に追加する方法

​​

オリジナル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

(HTTPあなたは[*最小限のが、完全な*]示すことが期待されていることに注意してください://stackoverflow.com/help/mcve)あなたが問題を解決するためのコードを他の人に依頼する前にあなたが今までに持っていたコード。基本コードを提供することで、人々はあなたがすでに知っていたこと、そしてあなたが助けが必要な特定の部分を見ることができます。そして、彼らは一からすべてを書く必要はありません。あなたを援助したい人には簡単にしてください。ありがとう – har07

答えて

1

を、あなたが投稿している必要があり、あなたはXMLを解析し、TO-XML LINQを、使用して新しい要素を作成する方法を知っていたと仮定追加する

ノード同じもののためのコードは、私たちはそれに基づいて答えを得ることができます:

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); 

実際の質問に答えるには、y OUは最後<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> 
関連する問題