469,928 Members | 1,871 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,928 developers. It's quick & easy.

XMLREADER or XMLDocument???

How do I read the attributes of this XML? I have a page with text boxes that
i want to read
these values in. notice there are 2 Parameter tags with the same attributes.
Code would help
<TranslationRecords>

<TranslationRecord TrxID="1">

<ParameterCollection>

<Parameter KeyName="FielDelimiterChar" KeyValue="29" />

<Parameter KeyName="SegmentDelimiterChar" KeyValue="30" />

</ParameterCollection>

</TranslationRecord>

</TranslationRecords>

Thanks


Nov 12 '05 #1
2 4560
Try this as a template. It should be generic enough to allow you to use it
on any XmlDocument to parse out
specific attrinutes.

Andy
using System;
using System.Xml;

namespace WalkingXMLNS
{
/// <summary>
/// This sample walks all the elements in an XmlDocument and reports on
every
/// Attribute it finds. The example is hardcoded but it can be loaded
from a
/// file or passed in as an argument of course.
/// </summary>

class WalkingXMLClass
{
[STAThread]
static void Main(string[] args)
{
XmlDocument originalDoc = new XmlDocument();
XmlNode currentNode;
XmlNode nextNode;

originalDoc.LoadXml("<TranslationRecords><Translat ionRecord
TrxID=\"1\"><ParameterCollection><Parameter KeyName=\"FielDelimiterChar\"
KeyValue=\"29\" /><Parameter KeyName=\"SegmentDelimiterChar\"
KeyValue=\"30\"
/></ParameterCollection></TranslationRecord></TranslationRecords>");

currentNode = originalDoc.FirstChild;

Console.WriteLine("--- Start of Document ---");

while (currentNode != null)
{
if (currentNode.Attributes != null)
{
foreach (XmlAttribute currentAttribute in
currentNode.Attributes)
{
Console.WriteLine("Element is " +
currentNode.LocalName +
"; Attribute is " + currentAttribute.LocalName +
"; Value is " + currentAttribute.Value);
}
}

nextNode = null;
if (currentNode.HasChildNodes)
{
//
// Step down a level.
//

nextNode = currentNode.FirstChild;
}
else if (currentNode.NextSibling != null)
{
//
// Next branch on the same level.
//

nextNode = currentNode.NextSibling;
}
else
{
//
// Have to step up one or more levels. Keep
// going until we either find a parent node
// with a 'next sibling' or until we run out
// of parents.
//

XmlNode tempNode1 = currentNode.ParentNode;;

while ((tempNode1 != null) && (nextNode == null))
{
if (tempNode1.NextSibling != null)
{
nextNode = tempNode1.NextSibling;
}
else
{
XmlNode tempNode2 = tempNode1.ParentNode;
tempNode1 = tempNode2;
}
}
}

currentNode = nextNode;
}

Console.WriteLine("--- End of Document ---");
}
}
}

<jo*****@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
How do I read the attributes of this XML? I have a page with text boxes that i want to read
these values in. notice there are 2 Parameter tags with the same attributes. Code would help
<TranslationRecords>

<TranslationRecord TrxID="1">

<ParameterCollection>

<Parameter KeyName="FielDelimiterChar" KeyValue="29" />

<Parameter KeyName="SegmentDelimiterChar" KeyValue="30" />

</ParameterCollection>

</TranslationRecord>

</TranslationRecords>

Thanks

Nov 12 '05 #2
jo*****@hotmail.com wrote:
How do I read the attributes of this XML? I have a page with text boxes that
i want to read
these values in. notice there are 2 Parameter tags with the same attributes.


Of course use XmlReader! Don't you want your app to be fast? Reading
attributes of Parameter elements is as simple as

XmlTextReader r = new XmlTextReader("foo.xml");
while (r.Read()) {
if (r.NodeType == XmlNodeType.Element && r.Name == "Parameter")
Console.WriteLine("Got Parameter! KeyName={0},
KeyValue={1}", r["KeyName"], r["KeyValue"]);
}

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

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Logician | last post: by
8 posts views Thread by Charles.Deisler | last post: by
2 posts views Thread by muesliflakes | last post: by
reply views Thread by Jeff | last post: by
2 posts views Thread by raj | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.