Selecting an Attribute within the SelectNode function.

C

Cheryl

I have problems selecting the attribute when I use the SelectNode function.

<book>
<title store='7456' >Nutshell</title>
<author id='1'>Drayton</author>
<xauthor publish = "1" publishtime = "2"/>
<author id='3'>Neward</author>
<author id='2'>Albahari</author>
</book>

**************************************************************

doc.Loadxml(dom);
XmlDocument doc = new XmlDocument(dom);
XmlNode docElement = doc.DocumentElement;
XmlNodeList result = SelectNodes("/book/title[@store='7456']")

**************************************************************
What I am trying to do is assign a variable to each tag attribute publishtime.
opublishtime = ??
 
L

Lars Wilhelmsen

Hi Cheryl,

Cheryl said:
I have problems selecting the attribute when I use the SelectNode function.

<book>
<title store='7456' >Nutshell</title>
<author id='1'>Drayton</author>
<xauthor publish = "1" publishtime = "2"/>
<author id='3'>Neward</author>
<author id='2'>Albahari</author>
</book>

doc.Loadxml(dom);
XmlDocument doc = new XmlDocument(dom);
XmlNode docElement = doc.DocumentElement;
XmlNodeList result = SelectNodes("/book/title[@store='7456']")

**************************************************************
What I am trying to do is assign a variable to each tag attribute
publishtime.
opublishtime = ??

First off, I believe this post belongs in the newsgroup
microsoft.public.dotnet.xml, but
what the heck:

Your XPath looks ok to me, but I'm not really sure what your meaning with
the last
sentence - do you want to assign something to the publishtime attribute on
the subelement
named xauthor? And (it may be a typo) the SelectNodes() call is not called
on an object instance.

Another thing, if the store attribute of title is unique (i.e. the "primary
key"), you could use
the SelectSingleNode() call instead to only select the node you want to
modify.

If so:
XmlElement xauthorElement =
doc.SelectSingleNode("/book/title[@store'7456']/xauthor") as XmlElement;
// this should be an XElement
if (xauthorElement != null)
{
xauthorElement.Attributes["publishtimes"] = 42;
}
....
// store the xml document (if needed).
doc.Save("foobar.xml");
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top