XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method
XML SelectSingleNode method XML SelectSingleNode method
XML SelectSingleNode method
Go Back  Xtreme Visual Basic Talk > > > XML SelectSingleNode method


Reply
 
Thread Tools Display Modes
  #1  
Old 02-25-2008, 02:59 AM
piggybank1974's Avatar
piggybank1974 piggybank1974 is offline
Ultimate Contributor
 
Join Date: Mar 2002
Location: weston-super-mare(UK)
Posts: 1,795
Question XML SelectSingleNode method


Hi There,

I though I would look into XML more, as i wanted to get rid of my Class that do something similar(add/modify etc)

I'm using my existing XML file that I created

Quote:
<Macrail>
<Setting Name="NormalButton" Font="Trebuchet MS, 9, Regular"/>
<Setting Name="CommandButton" Font="Trebuchet MS, 10, Bold Italic"/>
<Setting Name="Label" Font="Trebuchet MS, 8, Regular"/>
<Setting Name="TextBox" Font="Trebuchet MS, 8, Regular"/>
<Setting Name="ListView" CFont="Arial Black, 8, Regular" IFont="Trebuchet MS, 8, Italic" GridLines="True" OddColour="Gainsboro" EvenColour="Silver" BackColour="WhiteSmoke"/>
<Setting Name="LvwEvents" ColID="0" ColE_ID="0" ColE_Name="220" ColE_Start="140" ColE_Finish="139" ColE_JobRef="108" ColE_Company="0"/>
<Setting Name="LvwDiary" ColID="0" ColDairyID="0" ColEventID="0" ColUserID="0" ColDateCreated="120" ColDateModified="120" ColDairySubjectID="0" ColDairySubject="140" ColDescription="327" ColInfoFrom="145" ColRecordState="0" ColWebSiteID="0"/>
<Setting Name="LvwRegistry" ColID="0" ColRegistryID="0" ColUserID="0" ColFullName="110" ColSentinelID="85" ColCompanyID="0" ColCompany="115" ColTimeIn="130" ColTimeOut="130" ColActivityID="0" ColActivity="162" ColWorksite="0" ColSentinel="71" ColPPE="88" ColEventID="0" ColMobile="0" ColVehicle="0" ColName="0" Colorrname="0" Colorreason="0" ColTravelTime="0" Colexpglobal="0" Colexpcompany="0" ColRecordState="0" ColWebSiteID="0"/>
</Macrail>
I'm try to retrieve the 'Setting' Node that has the Name of Label

Code:
Dim dfg As Xml.XmlNode = XMLEle.SelectSingleNode("Macrail[Setting/Name='Label']")

that code just returns nothing.

the following code does work but only retrieves the first setting node that is <Setting Name="NormalButton" Font="Trebuchet MS, 9, Regular"/>

Code:
Dim dfg As Xml.XmlNode = XMLEle.SelectSingleNode("Setting")
Reply With Quote
  #2  
Old 02-25-2008, 08:18 AM
AtmaWeapon's Avatar
AtmaWeaponXML SelectSingleNode method AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Your xPath syntax is invalid; I always have to refer to a tutorial or the wikipedia page for xPath to get things right.

Basically, you are trying to access the name attribute of an element named Setting that is a child of the Macrail element. I'm not exactly sure why your xPath statement isn't throwing an exception; it's not valid at all. Let's discuss a few xPath concepts, then we can build an xPath statement that works.

Child nodes
Child nodes are separated by the '/' character; if you use a double slash ("//") at the beginning, the XPath statement means "find the node named x anywhere". So, to get to your Setting elements, you want an XPath that looks like one of the following:
Macrail/Setting or //Setting

Only use the second choice if you know it will find what you want.

Attributes
Attributes of an element are expressed using a syntax similar to C/C++ arrays, with the additional capability to index by name. When you refer to an attribute, it must always be prefixed with the '@' character. This is to distinguish elements from attributes.

For example, Setting[1] selects the first attribute of a Setting element, which seems to be Name in your XML file. Note that it would not be equivalent to use this XPath instead: Setting[@Name], which finds elements named Setting that have a Name attribute. You can use attributes to get specific as well: Setting[@Name='Label'] will only select elements named Setting that have a Name attribute with the value "Label". Attributes can also be selected directly; //@Name will select all attributes named Name in the file.

Note that the name is quoted with single quotes, and XPath does not support character escaping. I forget why this is important, but I remember really needing single quotes in an XPath one time and it was impossible. I was probably trying something stupid.

So, if we put it all together, our XPath should look like this:
Macrail/Setting[@Name='Label']

On an unrelated style note, the general XML convention is to PascalCase element names and camelCase attribute names; this makes it even easier to tell attributes and elements apart. You may want to consider this.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #3  
Old 02-25-2008, 09:01 AM
piggybank1974's Avatar
piggybank1974 piggybank1974 is offline
Ultimate Contributor
 
Join Date: Mar 2002
Location: weston-super-mare(UK)
Posts: 1,795
Question

Hi AtmaWeapon

I tried your subjection but still it returns nothing. Here is the full code I'm using, I think for about an hour I tried to work out why it was not working and i pritty sure i tried you subjection but I may have been wrong.

Code:
Dim XMLDoc As New Xml.XmlDocument Dim XMLEle As Xml.XmlElement XMLEle = XMLDoc.DocumentElement ' DisplayNodes(XMLEle) Dim dfg As Xml.XmlNode = XMLEle.SelectSingleNode("Macrail/Setting[@Name='Label']")

originaly I used these two classes(Uploaded) I build to handle similar to what XML can do easily I Think??

Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim reader As XmlTextReader = New XmlTextReader(Application.StartupPath & "\Xml\" & "Macrail.xml") Dim info() As String While (reader.Read()) Select Case reader.NodeType Case XmlNodeType.Element Console.Write("<" + reader.Name) If reader.Name = "Setting" Then If reader.AttributeCount > 0 Then Dim mSetting As New ClsSetting While (reader.MoveToNextAttribute()) If reader.Name = "Name" Then mSetting.SettingName = reader.Value Else mSetting.Add(reader.Name, reader.Value) End If Console.Write(reader.Name + " = " + reader.Value) End While mSettings.Add("Key" & mSetting.SettingName, mSetting) mSetting = Nothing End If End If Case XmlNodeType.EndElement Console.Write("</" + reader.Name + ">") 'break; End Select End While info = mSettings.Attribute("KeyNormalButton", "Font").Split(",") LSettings.mNormalButtonFont = Dialogs.ClsFont.CreateFont(info(0), Single.Parse(info(1)), info(2)) 'My.Settings.NormalButtonFont info = mSettings.Attribute("KeyCommandButton", "Font").Split(",") LSettings.mCommandButtonFont = Dialogs.ClsFont.CreateFont(info(0), Single.Parse(info(1)), info(2)) 'My.Settings.CommandButtonFont info = mSettings.Attribute("KeyLabel", "Font").Split(",") LSettings.mLabelFont = Dialogs.ClsFont.CreateFont(info(0), Single.Parse(info(1)), info(2)) 'My.Settings.LabelFont info = mSettings.Attribute("KeyTextBox", "Font").Split(",") LSettings.mTextBoxFont = Dialogs.ClsFont.CreateFont(info(0), Single.Parse(info(1)), info(2)) 'My.Settings.TextBoxFont info = mSettings.Attribute("KeyListView", "CFont").Split(",") LSettings.mColumnsFont = Dialogs.ClsFont.CreateFont(info(0), Single.Parse(info(1)), info(2)) 'My.Settings.ColumnsFont info = mSettings.Attribute("KeyListView", "IFont").Split(",") LSettings.mItemsFont = Dialogs.ClsFont.CreateFont(info(0), Single.Parse(info(1)), info(2)) 'My.Settings.ColumnsFont LSettings.mListViewGridlines = CBool(mSettings.Attribute("KeyListView", "GridLines")) 'My.Settings.ListViewGridlines Dim tc As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)) Dim newcolour As Color = DirectCast(tc.ConvertFromString(mSettings.Attribute("KeyListView", "BackColour")), Color) LSettings.mListViewColour = newcolour 'My.Settings.ListViewColour Dim tc1 As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)) Dim newcolour1 As Color = DirectCast(tc.ConvertFromString(mSettings.Attribute("KeyListView", "OddColour")), Color) LSettings.mListViewItemsOddColour = newcolour1 'My.Settings.ListViewItemsOddColour Dim tc2 As System.ComponentModel.TypeConverter = System.ComponentModel.TypeDescriptor.GetConverter(GetType(Color)) Dim newcolour2 As Color = DirectCast(tc.ConvertFromString(mSettings.Attribute("KeyListView", "EvenColour")), Color) LSettings.mListviewItemsEvenColour = newcolour2 'My.Settings.ListviewItemsEvenColour End Sub

Below is in a module

Code:
Public Structure StrucSettings Dim mNormalButtonFont As Font Dim mCommandButtonFont As Font Dim mSynchroniseTime As Int32 Dim mLabelFont As Font Dim mTextBoxFont As Font Dim mColumnsFont As Font Dim mItemsFont As Font Dim mListViewGridlines As Boolean Dim mListViewColour As Color Dim mListViewItemsOddColour As Color Dim mListviewItemsEvenColour As Color End Structure Public LSettings As New StrucSettings

I how for a fact this is not the best way to code this as it potentially resourse hungry, so i wanted to use XML Dom if i could and change attributes/value as need but i'm not to sure if this is the best way??

thanks for any ideas on this
Attached Files
File Type: vb ClsSetting.vb (1.7 KB, 1 views)
File Type: vb ClsSettings.vb (4.2 KB, 0 views)
Reply With Quote
  #4  
Old 02-25-2008, 09:28 AM
PrOpHeT's Avatar
PrOpHeTXML SelectSingleNode method PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

just my two cents, I have not looked in great detail.... But doesn't the "Macrail" need to be prefixed with "/" in order to specify a valid path? And are single quotes valid?

I use a different syntax and it works.

as I use it, it would now read...
Code:
Dim dfg As Xml.XmlNode = XMLEle.SelectSingleNode("/Macrail/Setting[@Name=""Label""]")
If not I am going to bow back out of this one...
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...

Last edited by PrOpHeT; 02-25-2008 at 09:36 AM.
Reply With Quote
  #5  
Old 02-25-2008, 09:35 AM
piggybank1974's Avatar
piggybank1974 piggybank1974 is offline
Ultimate Contributor
 
Join Date: Mar 2002
Location: weston-super-mare(UK)
Posts: 1,795
Default

Hi PrOpHeT,

Your subjection does seem to return the desired result, so dont bow out of this one just yet

I'm still not to sure if i can use the XML in the way i'm trying to use it.
Reply With Quote
  #6  
Old 02-25-2008, 09:47 AM
piggybank1974's Avatar
piggybank1974 piggybank1974 is offline
Ultimate Contributor
 
Join Date: Mar 2002
Location: weston-super-mare(UK)
Posts: 1,795
Default

I was just thinking the XML file itself may be not complex enough to do what i want to achieve in the long run.

I may have to create something like this:

Quote:
<Macrail>
<Settings Name ="Label">
<Font>Trebuchet MS, 8, Regular</Font>
<Colour>Silver"</Colour>
</Settings>
...
</Macrail>
again I'm not to sure if this is a good idea??
Reply With Quote
  #7  
Old 02-25-2008, 09:59 AM
PrOpHeT's Avatar
PrOpHeTXML SelectSingleNode method PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

Ok, what exactly is so complex it cannot be stored in XML? Remember entire classes can be serialized to XML if need be. When you combine that with CDATA, attributes, and binary fields stored as Base64 you can get just about anything into a XML file.
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
  #8  
Old 02-25-2008, 10:12 AM
AtmaWeapon's Avatar
AtmaWeaponXML SelectSingleNode method AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

PrOpHeT is actually right, but it means you didn't show me the real XML file.

A standard xml file should begin with an xml declaration that specifies at least the xml version; a common declaration looks like this:
<?xml version="1.0" encoding="utf-8" ?>

Your document did not include it, but it seems like from the viewpoint of the XmlDocument class this is OK.

XmlDocument.DocumentElement can return two things. If an XML declaration is present, the declaration is what's returned. If no XML declaration is present, apparently the first element node is returned. Since you are selecting from the context of the document element, it's important to know what that element is.

I was also using different code to get the setting element and bypassing the document element completely:
Code:
Dim dfg As XmlNode = xmlDoc.SelectSingleNode("Macrail/Setting[@Name='Label']")
This affects what the XPath statement needs to reflect. It's possible the statement should be prefixed with a "/", but it's working for me without.

If you're fishing for file design advice, piggybank1974, I can suggest some changes. Something that will prove difficult for parsing your file is the fact that there are several valid ways to describe a Setting element in your file. The Name attribute is common, but other than that you have a custom format for each Setting element. This would be painfully obvious if you were trying to write a schema to describe your document. In general, XML tags should have a well-defined format, and this is impossible in your current document. I do like the use of sub-elements instead of attributes, but I propose defining some special settings tags like so:
Code:
<ControlSetting Name="NormalButton">
  <Font>...</Font>
</ControlSetting>

<ListViewSetting Name="ListView">
  <CFont>...</CFont>
  <IFont>...</IFont>
  ...
</ListViewSetting>

<EventSetting Name="LvwEvents">
  <ColID>0</ColID>
  ...
</EventSetting>
This allows you to describe the document with a schema, and it also means that when you have the name of an element you know what child elements it should have. It's a lot easier than trying to remember what attributes go with each element.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #9  
Old 02-25-2008, 12:55 PM
piggybank1974's Avatar
piggybank1974 piggybank1974 is offline
Ultimate Contributor
 
Join Date: Mar 2002
Location: weston-super-mare(UK)
Posts: 1,795
Default

Hi Guys,

Thanks for your help the both of you here on this subject, XML is kind of new for me using it in .net as I've not needed to in the past, my AutoUpdater does use sub elements as you have described here, but this was a secondary development after this one, I'm going to improve it now on some of the recommendations that you described. I took out the <?xml version="1.0" encoding="utf-8" ?> as i though it was not really needed even though countless examples on the net and in my XML book do use it.

thanks again
Reply With Quote
  #10  
Old 02-25-2008, 01:02 PM
piggybank1974's Avatar
piggybank1974 piggybank1974 is offline
Ultimate Contributor
 
Join Date: Mar 2002
Location: weston-super-mare(UK)
Posts: 1,795
Default

I just though I still need a why to convert to a type(Color,font etc) I know I could have the following

[b]<Type>Font</Type>

Also I know about the GetConverter i mean if its a font type I only want to be able to return a font type and not a colour type. I may not be explaining myself to good sorry.
Reply With Quote
  #11  
Old 02-25-2008, 01:35 PM
PrOpHeT's Avatar
PrOpHeTXML SelectSingleNode method PrOpHeT is offline
Hopelessly confused...

* Expert *
 
Join Date: Mar 2001
Location: Tyler, Tx.
Posts: 3,055
Default

Well fonts can be stored as string variables as a new font declaration accepts strings as parameters when defining the new font.

Colors can be stored in XML easily enough using RGB or hex values.

Either as an object can be serialized as well and stored in the file as an actual object, not just the representation of.
__________________
When you earnestly believe you can compensate for a lack of skill by doubling your efforts, there's no end to what you can't do ;)

For the love of Gold...
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method XML SelectSingleNode method
XML SelectSingleNode method
XML SelectSingleNode method
 
XML SelectSingleNode method
XML SelectSingleNode method
 
-->