Reputation: 3415
I have the following XAML node
<TextBox x:Name="myTextContent"
MinWidth="278"
Panel.ZIndex="99"
TabIndex="3"
IsEnabled="True" Grid.ColumnSpan="2" Margin="47,106,47,247"
AutomationProperties.LabeledBy="{Binding ElementName=lblForTextContent}"
AutomationProperties.AutomationId="myForm_myTextContent"/>
I have a unit test in another class that checks to verify that the XAML node contains certain attributes (such as TabIndex) that we require in the XAML, but that attribute check should only be for certain element types, such as TextBox. In the unit test I want to ignore label elements.
How do I check that the node is 'TextBox' element?
I believe it may be something like node.NodeType == "TextBox"
or maybe node.NodeType.Equals("TexBox")
Here is some of my code I know this works, but I just need to make sure I EXCLUDE any node elements that are NOT 'Textbox', thus I need to know how to check the node.NodeType:
XmlAttribute attrTabIndex = node.Attributes["TabIndex"];
if (attrTabIndex == null)
{
if (node.InnerXml.Contains("TabIndex"))
{
result = true;
}
else
{
result = false;
}
}
else
{
result = true;
}
Upvotes: 0
Views: 142
Reputation: 712
I know testing against a XAML is different, however, as a quick test I just created an XML document with a root element and your TextBox node, and then used the code snippet below.
There's definitely more to code here, but maybe this will push you into the right direction with what you are trying to do.
XmlDocument xdoc = new XmlDocument() { PreserveWhitespace = true };
xdoc.Load("test.xml");
bool result = true;
foreach (var node in xdoc.DocumentElement.ChildNodes.OfType<XmlElement>().Where(n => n.Name == "TextBox"))
{
result = node.HasAttribute("TabIndex");
}
Upvotes: 0