all groups > dotnet xml > february 2005 >
You're in the

dotnet xml

group:

Getting the Attributesd of a single element


Getting the Attributesd of a single element qb
2/28/2005 7:46:55 AM
dotnet xml:
I'm trying to get the Attributes of an Element in my XML.
The proces I'm using works fine until I have ELements of the same name.

<TodaysPurchase>
<Order CustNum="123456" CustName="LName"....etc..>
<Order CustNum="123456" CustName="LName"....etc..>
<Order CustNum="123456" CustName="LName"....etc..>
</TodaysPurchase>

Again, when I have just 1 Element, works fine. But I'm unable to iterate through each element getting its attributes.

Thanks

For the help

qb
Re: Getting the Attributesd of a single element qb
2/28/2005 9:18:40 AM



Hello Martin,

Currently I using a foreach on the attributes from an XmlDocumente Object

foreach(XmlAttribute xa in xNode.Attributes)

This obviously fills the Attributes collecetion with all attributes from the doc, hence my problem.

[quoted text, click to view]
Re: Getting the Attributesd of a single element qb
2/28/2005 9:39:16 AM
Hello Martin,

Thanks for the quick reply. I really should read through the documentation thoroughly. I only glanced at that method.
Anyway, you did save me some time. And I learned something along the way. I do appreciate you assistance!!!!!


qb


[quoted text, click to view]
Re: Getting the Attributesd of a single element Martin Honnen
2/28/2005 6:07:49 PM


[quoted text, click to view]

What are you using/do you want to use, DOM, XPath, XmlTextReader?

For instance using an XPathDocument and an XPathNavigator you could do
the following in C#:

XPathDocument xpathDocument = new XPathDocument(@"test2005022802.xml");
XPathNavigator navigator = xpathDocument.CreateNavigator();
XPathNodeIterator orders = navigator.Select(@"//Order");
while (orders.MoveNext()) {
Console.WriteLine("Accessing {0}: ", orders.Current.Name);
XPathNavigator attributeNavigator = orders.Current.Clone();
if (attributeNavigator.MoveToFirstAttribute()) {
Console.WriteLine("Attribute {0} has value {1}.",
attributeNavigator.Name, attributeNavigator.Value);
};
while (attributeNavigator.MoveToNextAttribute()) {
Console.WriteLine("Attribute {0} has value {1}.",
attributeNavigator.Name, attributeNavigator.Value);
}
Console.WriteLine();
}


--

Martin Honnen
Re: Getting the Attributesd of a single element Martin Honnen
2/28/2005 6:33:31 PM


[quoted text, click to view]


[quoted text, click to view]

DOM would work as follows:

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(@"test2005022802.xml");
foreach (XmlNode order in xmlDocument.GetElementsByTagName("Order")) {
Console.WriteLine("Found node {0}: ", order.Name);
foreach (XmlAttribute attribute in order.Attributes) {
Console.WriteLine("Attribute {0} has value {1}.",
attribute.Name, attribute.Value);
}
Console.WriteLine();
}

--

Martin Honnen
AddThis Social Bookmark Button