Reputation: 17418
I am trying to extract all id attributes (as List) where the string starts with ###. This is my first attempt:
const string xml = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Actions>
<Action id=""###SignIn"">1</Action>
<Action id=""SignOut"">2</Action>
<Action id=""###Open"">3</Action>
</Actions>";
var xdoc = XDocument.Parse(xml);
var test = xdoc.Root.Elements()
.Where(e => e.Attribute("id").Value.StartsWith("###"))
.Select(e => e.Attribute("id").Value.StartsWith("###")).ToList();
Ultimately, I would like to replace those string with values in a dictionary (the key values are the same as the strings found in the xm data).
Upvotes: 0
Views: 60
Reputation: 89315
A slight modification to the Select()
part of your attempt should do :
var xdoc = XDocument.Parse(xml);
var test = xdoc.Root
.Elements()
.Where(e => e.Attribute("id").Value.StartsWith("###"))
.Select(e => e.Attribute("id")).ToList();
foreach (var t in test)
{
//replace id attribute value with string from dictionary
t.Value = myDictionary[t.Value];
}
Upvotes: 2