473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Parsing XML nodes by Attribute

I have this simple xml file which wont ever get beyong 3 nodes deep. I need
to get the value of the child node of any node with an id of '63'. So fir
instance I would be returned 'False' from the example below.

Ive attached an XML snippet and a cC# code snippet. The piece of code is not
working as I am not too familiour with parsing XML files.I think I need to
load the attributes into an array and loop through them to check for that
value. Could anyone help me with this? Is there an wasier way?

Thanks,
Grant

-------Sample XML-----------------

<session-data>
<attr-inst id="p1" state="known">
<val>Wilma Flintstone</val>
</attr-inst>
<attr-inst id="63" state="known">
<val>false</val>
</attr-inst>
</session-data>

----------Sample C# Code------------

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList nodeList = xmlDoc.SelectNodes("//session-data/attr-inst");
foreach (XmlNode node in nodeList)
{
if ( node.Attributes["id"].Attributes == "63")
{
node.Attributes["id"].FirstChild.Value = newDate;
}
}
Nov 16 '05 #1
5 12267


Grant wrote:
I have this simple xml file which wont ever get beyong 3 nodes deep. I need
to get the value of the child node of any node with an id of '63'. So fir
instance I would be returned 'False' from the example below.


If you only want to read out values then XPathDocument is one way to do
it, it is easy to write one XPath expression selecting the nodes as
described above:

XPathDocument xpathDocument = new XPathDocument(@"test2005031602.xml");
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathNodeIterator nodeIterator =
xpathNavigator.Select(@"/session-data/attr-inst[@id = '63']/*");
while (nodeIterator.MoveNext()) {
Console.WriteLine("Found value \"{0}\".",
nodeIterator.Current.Value);
}

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 16 '05 #2
I managed to find a way of going directly to that specific node using the
code below. What I need to do now is modify the child node value that it
picks up. Problem is it is wiping out the '<val>' and '</val>'part of the
node. So when I run the code from the code snippet section below, against
the Original XML snippet, I get the modified XML output. You can see it has
deleted the child node and simply added the new value as a value of its
parent node.

how do I simply change the value of that node Ive found?
Thanks,
Grant
------Original XML-----------
<attr-inst id="63" state="known">
<val>false</val>
</attr-inst>

------modified XML-----------
<attr-inst id="63" state="known">true</attr-inst>

--------Code Snippet------------
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList nodeList = xmlDoc.SelectNodes("//session-data/attr-inst");
XmlNode findnode;
XmlElement root = xmlDoc.DocumentElement;
findnode = root.SelectSingleNode("descendant::attr-inst[@id='63']");
findnode.InnerXml = "true";
xmlDoc.Save(fileName);

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:ei**************@TK2MSFTNGP14.phx.gbl...


Grant wrote:
I have this simple xml file which wont ever get beyong 3 nodes deep. I
need to get the value of the child node of any node with an id of '63'.
So fir instance I would be returned 'False' from the example below.


If you only want to read out values then XPathDocument is one way to do
it, it is easy to write one XPath expression selecting the nodes as
described above:

XPathDocument xpathDocument = new
XPathDocument(@"test2005031602.xml");
XPathNavigator xpathNavigator = xpathDocument.CreateNavigator();
XPathNodeIterator nodeIterator =
xpathNavigator.Select(@"/session-data/attr-inst[@id = '63']/*");
while (nodeIterator.MoveNext()) {
Console.WriteLine("Found value \"{0}\".",
nodeIterator.Current.Value);
}

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #3


Grant wrote:
I managed to find a way of going directly to that specific node using the
code below. What I need to do now is modify the child node value that it
picks up. Problem is it is wiping out the '<val>' and '</val>'part of the
node. So when I run the code from the code snippet section below, against
the Original XML snippet, I get the modified XML output. You can see it has
deleted the child node and simply added the new value as a value of its
parent node.


You do not access the right node, as suggested use the XPath expression
@"/session-data/attr-inst[@id = '63']/*"
then you have the element child nodes you are looking for, perhaps if
you know the element name of the child node you want
@"/session-data/attr-inst[@id = '63']/val"
then you can use SelectSingleNode and change
node.InnerText = "true";
Or access the text node itself e.g.
@"/session-data/attr-inst[@id = '63']/val/text()"
then use
node.Value = "true";

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 16 '05 #4
That works great - So its simply standard Xpath expressions that can be used
to browse nodes, exactly the same as XSLT.

Thanks for your help,
Grant

"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...


Grant wrote:
I managed to find a way of going directly to that specific node using the
code below. What I need to do now is modify the child node value that it
picks up. Problem is it is wiping out the '<val>' and '</val>'part of the
node. So when I run the code from the code snippet section below, against
the Original XML snippet, I get the modified XML output. You can see it
has deleted the child node and simply added the new value as a value of
its parent node.


You do not access the right node, as suggested use the XPath expression
@"/session-data/attr-inst[@id = '63']/*"
then you have the element child nodes you are looking for, perhaps if you
know the element name of the child node you want
@"/session-data/attr-inst[@id = '63']/val"
then you can use SelectSingleNode and change
node.InnerText = "true";
Or access the text node itself e.g.
@"/session-data/attr-inst[@id = '63']/val/text()"
then use
node.Value = "true";

--

Martin Honnen
http://JavaScript.FAQTs.com/

Nov 16 '05 #5
Your findnode has a child node below it <val> so you should be
modifying the value of that.
Try replacing
findnode.InnerXml = "True";
with
XmlElement val_node = (XmlElement)findnode.FirstChild;
val_node.NodeValue = "True;

Nov 16 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Luis P. Mendes | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I only know a little bit of xml and I'm trying to parse a xml document in order to save its elements in a file (dictionaries inside a list)....
2
by: AR | last post by:
Hi, I have an xml like this: <a> a 1 <b font-size="10">b 2</b> x <c font-size="14" font-weight="bold">c 3</c> y </a>
4
by: Erik Moore | last post by:
I am both producing and parsing an xml document that needs to be validated against a schema. I wanted some consumers of the document to have the option of not performing a validation, so I left the...
2
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
6
by: Grant | last post by:
I have this simple xml file which wont ever get beyong 3 nodes deep. I need to get the value of the child node of any node with an id of '63'. So fir instance I would be returned 'False' from the...
8
by: Fabio Cannizzo | last post by:
I have an XML file based on an XSD schema. It represents the content of a treeview (or a menu system), where the nodes are always of the same few types, but can be arbitrarily nested. I am...
4
by: Mike [MCP VB] | last post by:
Hi, I hope this is the right place to ask. I have an xml file thus: <?xml version="1.0" encoding="UTF-8"?> <Message xmlns="urn:myurl-org:ns2"...
5
by: WTH | last post by:
In C++ I had, long ago, written my own XML parsing class as I never found even a half decent node based hierarchical solution that was simple, now that I'm starting to develop tools in C# (as a...
2
by: nicky123 | last post by:
Hi everyone, This is a brief description that I have provided for parsing & displaying an XML document using DOM API. Please feel free to post your own comments & views regarding...
0
by: gnewsgroup | last post by:
I need to bind *some* nodes of an xml document to an asp.net Menu control. The problem is that the XML document uses the same node name and attribute name for for the entire document among all...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.