Help in selecting attributes with XPath [modified]
-
<field name="textBox" y="3.175" x="3.17" w="62" h="9"> <ui> <textBox> <border> <css style=""> </css> </border> <margin/> </textBox> </ui> <font face="Arial"/> <margin topInset="1" bottomInset="1" leftInset="1" rightInset="1"/> <textAlign vAlign="middle"/> <label reserve="25"> <Font fontFace="Arial" size="8pt" /> <textAlign vAlign="middle"/> <value> <text>textbox label</text> </value> </label> </field>
So I am trying to read the values of Font (IF there is a textBox element present) and trying to read the attributes fontFace and size, here is my Xpath:
XPathNodeIterator iterator = nav.Select(@"/field"); try { while (iterator.MoveNext()) { //Do some other stuff ... XPathNavigator nav2 = iterator.Current.Clone(); if (string.Compare(nav2.Name, "field") == 0) { XPathNodeIterator textBoxIterator = nav2.Select("child::*/child::textBox"); if (textBoxIterator.Count > 0) { XPathExpression exp = nav2.Compile("child::Font"); XPathNodeIterator it = nav2.Select(exp); while (it.MoveNext()) { XPathNavigator nav3 = it.Current.Clone(); string fontFace = nav3.GetAttribute("typeface", nav3.NamespaceURI); string fontSize = nav3.GetAttribute("size", nav3.NamespaceURI); } } } } catch ... }
Am I doing it the proper way or is it too complicated and can be simplified?
modified on Tuesday, March 4, 2008 10:22 AM
-
<field name="textBox" y="3.175" x="3.17" w="62" h="9"> <ui> <textBox> <border> <css style=""> </css> </border> <margin/> </textBox> </ui> <font face="Arial"/> <margin topInset="1" bottomInset="1" leftInset="1" rightInset="1"/> <textAlign vAlign="middle"/> <label reserve="25"> <Font fontFace="Arial" size="8pt" /> <textAlign vAlign="middle"/> <value> <text>textbox label</text> </value> </label> </field>
So I am trying to read the values of Font (IF there is a textBox element present) and trying to read the attributes fontFace and size, here is my Xpath:
XPathNodeIterator iterator = nav.Select(@"/field"); try { while (iterator.MoveNext()) { //Do some other stuff ... XPathNavigator nav2 = iterator.Current.Clone(); if (string.Compare(nav2.Name, "field") == 0) { XPathNodeIterator textBoxIterator = nav2.Select("child::*/child::textBox"); if (textBoxIterator.Count > 0) { XPathExpression exp = nav2.Compile("child::Font"); XPathNodeIterator it = nav2.Select(exp); while (it.MoveNext()) { XPathNavigator nav3 = it.Current.Clone(); string fontFace = nav3.GetAttribute("typeface", nav3.NamespaceURI); string fontSize = nav3.GetAttribute("size", nav3.NamespaceURI); } } } } catch ... }
Am I doing it the proper way or is it too complicated and can be simplified?
modified on Tuesday, March 4, 2008 10:22 AM
//field/font@fontFace I believe, and you can put something in [] to specific a condition, such as where there's a sibling node called textbox.
Christian Graus - Microsoft MVP - C++ "also I don't think "TranslateOneToTwoBillion OneHundredAndFortySevenMillion FourHundredAndEightyThreeThousand SixHundredAndFortySeven()" is a very good choice for a function name" - SpacixOne ( offering help to someone who really needed it ) ( spaces added for the benefit of people running at < 1280x1024 )