Part of the XML content:
<section name="Header"> <placeholder name="HeaderPane"></placeholder> </section> <section name="Middle" split="20"> <placeholder name="ContentLeft" ></placeholder> <placeholder name="ContentMiddle"></placeholder> <placeholder name="ContentRight"></placeholder> </section> <section name="Bottom"> <placeholder name="BottomPane"></placeholder> </section>
I want to check in each node and if attribute split
exist, try to assign an attribute value in a variable.
Inside a loop, I try:
foreach (XmlNode xNode in nodeListName) { if(xNode.ParentNode.Attributes["split"].Value != "") { parentSplit = xNode.ParentNode.Attributes["split"].Value; } }
But I'm wrong if the condition checks only the value, not the existence of attributes. How should I check for the existence of attributes?
How to check if xml node exists? To verify if node or tag exists in XML content, you can execute an xpath expression against DOM document for that XML and count the matching nodes. matching nodes > zero – XML tag / attribute exists. matching nodes <= zero – XML tag / attribute does not exist.
Attribute Types: There are three types of attributes described below: String types Attribute: This type of attribute takes any string literal as a value.
XML elements can have attributes, just like HTML. Attributes are designed to contain data related to a specific element.
You can actually index directly into the Attributes collection (if you are using C# not VB):
foreach (XmlNode xNode in nodeListName) { XmlNode parent = xNode.ParentNode; if (parent.Attributes != null && parent.Attributes["split"] != null) { parentSplit = parent.Attributes["split"].Value; } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With