1

I want to remove a specific value from the following XDocument structure:

<plist version="1.0">
<dict>
<key>Main</key>
<array>
    <dict>
        <key>Password</key>
        <string>*********</string>
        <key>Username</key>
        <string>testuser</string>
    </dict>
</array>
<key>Profile</key>
<string>test profile 1</string>
</dict>
</plist>

Suppose I want to remove the string value associated with key=Password, how can I do that?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
gauravhalbe
  • 392
  • 2
  • 8
  • 19
  • 2
    Do you have any code that you've tried to do this already? – Cam Bruce Aug 05 '14 at 22:33
  • This link can help you : http://stackoverflow.com/questions/2558787/how-to-modify-exiting-xml-file-with-xmldocument-and-xmlnode-in-c-sharp – user2274060 Aug 05 '14 at 22:37
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Aug 06 '14 at 00:28
  • thanks for the update John. That should help me with future questions! – gauravhalbe Aug 06 '14 at 14:33

1 Answers1

2

You can use an XPath to retrieve the password elements and reset their contents:

var passwords = document.XPathSelectElements("//key[text()='Password']/following-sibling::string[1]");
foreach(XNode elem in passwords)
{
  elem.SetValue(string.Empty);
}

Of course, next you have to save the document back.

What it does is identify Password key elements and then the immediate next sibling. This assumes that the order is always the same as in your example: first key, then string.

You can check it here, online.

And, of course, there's this solution with Linq to XML.

Community
  • 1
  • 1
Marcel N.
  • 13,726
  • 5
  • 47
  • 72
  • I don't see anything in your code that references the `` element. The passwords are not embedded XNodes in the `` element. – Robert Harvey Aug 05 '14 at 22:39