I have a portion of XAML string in code-behind like this
string str = "<Button Name = \"btn1\" Foo = \"Bar"\ /><TextBox Name=\"txtbox1\">"
What should be the regex to find value of only Name attributes.
I want
btn1
txtbox1
How?
I have a portion of XAML string in code-behind like this
string str = "<Button Name = \"btn1\" Foo = \"Bar"\ /><TextBox Name=\"txtbox1\">"
What should be the regex to find value of only Name attributes.
I want
btn1
txtbox1
How?
And you definitely don't want to do this?
string str = "<Button Name = \"btn1\" /><TextBox Name=\"txtbox1\"/>";
var attrs = XElement.Parse("<r>"+str+"</r>").Elements().Attributes("Name").Select(a => a.Value);
foreach (var attr in attrs) Console.WriteLine(attr);
Instead of LINQ, you can also use XPath to get the value. This will get you the name of the first Button:
string str = "<Button Name = \"btn1\" /><TextBox Name=\"txtbox1\"/>";
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root>" + str + "</root>");
string name = doc.SelectSingleNode("root/Button/@Name").InnerText;
Or, if you just want to get the first name attribute for any item:
string name = doc.SelectSingleNode("root/*/@Name").InnerText;
Or to get a list of all the name attributes for all items:
foreach (XmlNode node in doc.SelectNodes("root/*/@Name"))
{
string name = node.InnerText';
}
etc.