473,385 Members | 1,309 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,385 software developers and data experts.

how to get value of XML tag attribute in C#?

Hi,
my xml is:
<ROOT>
<FILE NAME="filename">CDATA</FILE>
</ROOT>

this xml is argument to a function in C#.
i want to get the filename in a string variable.
how do i do it???
GetElementByTagname("FILE)[0].Attribute retrieves attributelist
how do i get the value of attribute????

Sep 29 '06 #1
3 2742
like this:

XmlTextReader tr = new XmlTextReader(filename);
while (tr.Read())
{
if (tr.NodeType == XmlNodeType.Element)
{
switch (tr.LocalName)
{

case ("FILENAME"):
_fileName= int.Parse(tr.ReadString());
break;
}
}
}

but your xml would need to look like this:

<ROOT>
<FILENAME>CDATA</FILENAME>
</ROOT>

that would do it.
"Saurabh" <on***********@gmail.comwrote in message
news:11*********************@e3g2000cwe.googlegrou ps.com...
Hi,
my xml is:
<ROOT>
<FILE NAME="filename">CDATA</FILE>
</ROOT>

this xml is argument to a function in C#.
i want to get the filename in a string variable.
how do i do it???
GetElementByTagname("FILE)[0].Attribute retrieves attributelist
how do i get the value of attribute????

Sep 29 '06 #2
Thanks Daniel, i learned a new way to access XML.
The problem is that the xml has to look like:
<ROOT>
<FILE NAME="filename"></FILE>
</ROOT>

beacuse the xml is generated by a recursive function which scans the
file system (DirectoryInfo ans FileInfo class) and generate a xml. The
exact xxml format is:
<ROOT>
<DIRECTORY>
<FILE NAME="filename">CDATA contains filestream</FILE>
<FILE NAME="filename">CDATA contains filestream</FILE>
</DIRECTORY>
<DIRECTORY>
<FILE NAME="filename">CDATA contains filestream</FILE>
<FILE NAME="filename">CDATA contains filestream</FILE>
</DIRECTORY>
</ROOT>

you never know if the filename can be same with different extension.
just to be precautious i have made the file tag with filename as
attribute.

Sep 30 '06 #3
GVN
Hi Saurabh,

I hope the following code should help you.

XmlDocument _Document = new XmlDocument();
XmlNodeList properties;
_Document.Load (XmlFileName);
XmlNodeList properties = _Document["Design"].ChildNodes;
foreach (XmlNode property in properties)
{
if(property.HasChildNodes)
{
XmlNodeList childNodes = property.ChildNodes;
foreach(XmlNode childNode in childNodes)
{
MessageBox.Show(childNode.Attributes["FILE"].Value); // You can
show file name.
}
}
}

Thanks,
Muralidhar GVN

Saurabh wrote:
Thanks Daniel, i learned a new way to access XML.
The problem is that the xml has to look like:
<ROOT>
<FILE NAME="filename"></FILE>
</ROOT>

beacuse the xml is generated by a recursive function which scans the
file system (DirectoryInfo ans FileInfo class) and generate a xml. The
exact xxml format is:
<ROOT>
<DIRECTORY>
<FILE NAME="filename">CDATA contains filestream</FILE>
<FILE NAME="filename">CDATA contains filestream</FILE>
</DIRECTORY>
<DIRECTORY>
<FILE NAME="filename">CDATA contains filestream</FILE>
<FILE NAME="filename">CDATA contains filestream</FILE>
</DIRECTORY>
</ROOT>

you never know if the filename can be same with different extension.
just to be precautious i have made the file tag with filename as
attribute.
Sep 30 '06 #4

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

Similar topics

3
by: TekWiz | last post by:
I've got a system that automatically generates a form. I have it set up so that the backend will return to the inital form page with an error object in sessions data (assuming the backend detected...
4
by: Lénaïc Huard | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello, I've some namespace problems when defining default values for attributes. My problem seems to come from the fact that the attributes are...
2
by: Eshrath | last post by:
Hi, What I am trying to do: ======================= I need to form a table in html using the xsl but the table that is formed is quite long and cannot be viewed in our application. So we are...
2
by: TadPole | last post by:
Hi all, My main problems are::::::::: 1. Set a value within a block container that can be used and changed by subsequent templates/block-containers/tables etc.. 2. get/determine/find the...
10
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me,...
5
by: Simon | last post by:
Hi all, I am writing a windows application using vb.net on the 1.1 framework. We have in the application, some strongly typed collections that have been written as classes that do not inherit...
8
by: patrizio.trinchini | last post by:
Hi All, I'would like to write an XSL transformation that changes the value of the atribute of a given element according to the value of another atttribute of the same element. For instance,...
3
by: patrizio.trinchini | last post by:
Hi, how can remove sibling elements based on the value of an attribute ? For instance, gven the XML document: <root> <parentElment> <testElement name="A"> <removableElement/>
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.