Reputation: 402
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?
Upvotes: 1
Views: 57
Reputation: 13976
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.
Upvotes: 2