473,508 Members | 2,361 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read XML element value

Hello.

I have such XML file:
<root>
<elem1>
<optional>
Some data
<optional>
</elem1>
</root>
How can I get value of the "optional" node?

Thank you.
Nov 17 '05 #1
4 22245
Hello,
Load the XML file in a XMLDocument object and get the desired not using
XPath query.

XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNode node = (XmlNode)doc.DocumentElement;
XmlNode optionalNode = node.SelectSingleNode("root/elem1");

check if optional not is not null before using it.

HTH. Cheers.
Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #2
Hello,
Load the XML file in a XMLDocument object and get the desired not using
XPath query.

XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNode node = (XmlNode)doc.DocumentElement;
XmlNode optionalNode = node.SelectSingleNode("root/elem1");

check if optional not is not null before using it.

HTH. Cheers.
Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Recommenting: Load the XML file in a XMLDocument object and get the
desired *NODE* using
XPath query.

Sorry for the typo :)

Maqsood Ahmed [MCP C#,SQL Server]
Kolachi Advanced Technologies
http://www.kolachi.net

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #4


David Dvali wrote:

I have such XML file:
<root>
<elem1>
<optional>
Some data
<optional> ^^^^^^^^^]
Needs to be </optional>
</elem1>
</root>
How can I get value of the "optional" node?


You can use XPath to find whether the <optional> element exists e.g.
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"file.xml");
XmlElement optional =
xmlDocument.SelectSingleNode(@"/root/elem1/optional") as XmlElement;
if (optional != null) {
Console.WriteLine("InnerText is {0}", optional.InnerText);
}
else {
Console.WriteLine("Element not found.");
}

Whether the InnerText gives you what you consider the "element value" I
don't know, lots of people have different opinions on that constitutes
the value of an element in an XML document.
For your above example the InnerText gives you the complete text
including white space that is in the <optional> element e.g. InnerText is
"
Some data
"

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 17 '05 #5

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

Similar topics

3
7242
by: reneeccwest | last post by:
<input type="text" size="7" name="test" maxlength="4" value="4Tyourname"> Is there any way I can make first 2 characters as a read only value?
4
24421
by: Randell D. | last post by:
Folks, I have a form (called FORM1) - In my INPUT submit tag, I have an onClick event that I have successfully tested/used to display the value of a TEXT box using the following function (called...
2
3072
by: worli | last post by:
Hi All, I have a strange requirement. I have a dynamic input numeric data stream e.g. 2, 2, 4, 5 etc.... ( each input number range from 1 to 10 ). lets take a simple case where all inputs will...
2
1701
by: Rich | last post by:
I am very new to ASP.NET, and did not see a similar error posted anywhere. Here is my situation. I followed the instructions in Peter Bromberg's excellent article "Host Winforms - based...
2
3550
by: luthriaajay | last post by:
I need some help to extract the LatestFillQuantity element value using XPATH. in Java. I am unable to extract the value of 10000. Please help as to what have I done wrong.? Help appreciated. ...
11
2625
by: xz | last post by:
>From the reference of MSDN about hash map: operator Inserts an element into a hash_map with a specified key value. And such example is given: hash_map <int, inthm1; hm1 = 40;
3
10205
by: thomas | last post by:
I want to use a priority_queue like STL data structure. But I found that priority_queue cannot update element value: only pop/ push is supported. I'm using priority_queue to implement the prim...
2
1587
by: MATTXtwo | last post by:
I want to get input element value in a form not using Javascritp like: var NICNo=document.getElementById("NewICNo").value;
2
2503
by: ramuksasi | last post by:
Hi, I am unable to read a cookie value from the cookies. i have set cookie value from javascript in scripting side and try to read cookie value from coding part ie. default.aspx.vb. i have shown...
0
7223
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
7115
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7377
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...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4705
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.