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.

Get Xml attribute

<xml>
<test attrib1="hello" attrib2="world">textValue</test>
</xml>

I need some help getting the values of attrib1, attrib2 and the test tag.
the output should be:
hello
world
textValue

[c#]
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlNode docElement = doc.DocumentElement;
XmlAttributeCollection tagAttribute = docElement.Attributes;

I got to this far but not sure what to do next

Thanks in advance

Aaron
Nov 12 '05 #1
5 43771
Aaron wrote:
<xml>
<test attrib1="hello" attrib2="world">textValue</test>
</xml>

I need some help getting the values of attrib1, attrib2 and the test tag.
the output should be:
hello
world
textValue

[c#]
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlNode docElement = doc.DocumentElement;
XmlAttributeCollection tagAttribute = docElement.Attributes;


In your document DocumentElement is xml element (btw, that's reserved
name, don't use it) and it doesn't have attributes.
I'd suggest you using XPath instead of native DOM navigation even in
such simple case, because simple usually become convolute and modifying
DOM navigation code could be a real nightmare.

Use
XmlNodeList nodes = doc.SelectNodes("/xml/test/@* | /xml/test/text()");
foreach(XmlNode node in nodes) {
Console.WriteLine(node.Value);
}

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #2
Do you know how to get the attribute values?

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:eY**************@TK2MSFTNGP09.phx.gbl...
Aaron wrote:
<xml>
<test attrib1="hello" attrib2="world">textValue</test>
</xml>

I need some help getting the values of attrib1, attrib2 and the test tag. the output should be:
hello
world
textValue

[c#]
XmlDocument doc = new XmlDocument();
doc.Load(url);
XmlNode docElement = doc.DocumentElement;
XmlAttributeCollection tagAttribute = docElement.Attributes;


In your document DocumentElement is xml element (btw, that's reserved
name, don't use it) and it doesn't have attributes.
I'd suggest you using XPath instead of native DOM navigation even in
such simple case, because simple usually become convolute and modifying
DOM navigation code could be a real nightmare.

Use
XmlNodeList nodes = doc.SelectNodes("/xml/test/@* | /xml/test/text()");
foreach(XmlNode node in nodes) {
Console.WriteLine(node.Value);
}

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #3
Aaron wrote:
Do you know how to get the attribute values?


Sorry, did you see the code I've posted? It does outputs attribute values.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #4
i see this but this is only one value isn't it?

Console.WriteLine(node.Value);

could you set the output to variables? sorry im new to xml.

pseudo-code
attrib1 = node.attrib1
attrib2 = node.attrib2
textValue = node.value

thx

"Oleg Tkachenko" <oleg@NO!SPAM!PLEASEtkachenko.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Aaron wrote:
Do you know how to get the attribute values?


Sorry, did you see the code I've posted? It does outputs attribute values.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

Nov 12 '05 #5
Aaron wrote:
i see this but this is only one value isn't it?

Console.WriteLine(node.Value);
Oh come on, don't you see this statement is within a foreach loop over
selected nodes (attributes and text):

XmlNodeList nodes = doc.SelectNodes("/xml/test/@* | /xml/test/text()");
foreach(XmlNode node in nodes) {
Console.WriteLine(node.Value);
}

It does outputs
hello
world
textValue

could you set the output to variables? sorry im new to xml.

pseudo-code
attrib1 = node.attrib1
attrib2 = node.attrib2
textValue = node.value


Sure
XmlElement test = (XmlElement)doc.SelectSingleNode("/xml/test");
string attrib1 = test.Attributes["attrib1"].Value;
string attrib2 = test.Attributes["attrib2"].Value;
string textValue = test.FirstChild.Value;

or

XmlElement test = (XmlElement)doc.SelectSingleNode("/xml/test");
string attrib1 = doc.SelectSingleNode("/xml/test/@attrib1").Value;
string attrib2 = doc.SelectSingleNode("/xml/test/@attrib2").Value;
string textValue = doc.SelectSingleNode("/xml/test/text()").Value;

--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog
Nov 12 '05 #6

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

Similar topics

7
by: svilen | last post by:
hello again. i'm now into using python instead of another language(s) for describing structures of data, including names, structure, type-checks, conversions, value-validations, metadata etc....
1
by: j erickson | last post by:
with the following xsl and xml file, the display of the gif file with the <image/url> tag works. However, the gif file in the <description> tag using the name attribute "src" won't make the correct...
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...
3
by: Tony Johansson | last post by:
Hello!! Assume we have one base class called Vehicle and two derived classes called Car and Bus. I would be able to call method getName on an object of class Car or Bus and return back the name...
1
by: arnold | last post by:
Hi, I've been knocking my head against the wall trying to create an XSL transform to perform "normalizations" of a set of XML files that have a common structure. % XML file before transform
5
by: Soledad Vel | last post by:
Hi All, i write this code: var sliderwidth=100; var sliderheight = 100; var div1 = document.createElement('div'); div1.setAttribute('id','d5'); div1.setAttribute('style',...
2
by: gary.goodwin | last post by:
HI I am trying to understand Attribute usage. For example the class SerializableAttribute is a class correct? So why when it is actually u sed the "Attribute" portion of the name is dropped. The...
0
by: Thomas Wittek | last post by:
Hi! I'm using xsl:attribute-sets to reduce redundancy in my XSLT. An example from a transformation to XHTML (the attribute values are simply copied from input to output): <xsl:attribute-set...
6
by: Adam Donahue | last post by:
As an exercise I'm attempting to write a metaclass that causes an exception to be thrown whenever a user tries to access 'attributes' (in the traditional sense) via a direct reference. Consider:...
18
by: Gabriel Rossetti | last post by:
Hello everyone, I had read somewhere that it is preferred to use self.__class__.attribute over ClassName.attribute to access class (aka static) attributes. I had done this and it seamed to work,...
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: 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...
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
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
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.