Problem in writing a method to get XML element's text and attribute in one go

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Dear Friends,<br/>
<br/>
I am trying to make a method so anytime by entering two arguments it will return text and attribute of an element in a XML file.<br/>
<br/>
Here it goes, it return the text value but I have troubles returning attribute:
<pre lang="x-c# /// <summary>
/// Get content of an element and optionally its attributes (Saves in an array of size 1 or 2)
/// </summary>
/// <param name="tagName
/// REQUIRED
/// string
/// Name of the elements tag
/// </param>
/// <param name="attributeName
/// OPTIONAL
/// string
/// Name of the attribute to get its value
/// </param>
/// <returns></returns>
public string[] getContent(string tagName, string attributeName = null)
{
XmlTextReader reader = new XmlTextReader("c:\post_fail.xml");

int arrayLength = 1;

if(attributeName != null)
{
arrayLength = 2;
}

string[] content = new string[arrayLength];

while (reader.Read())
{
if (reader.Name == tagName)
{
content[0] = reader.ReadString();

if (attributeName != null)
{
reader.MoveToNextAttribute();
content[1] = (string)reader.GetAttribute(attributeName);
}

break;
}
}

return content;
}[/code]
And this is an example how to call the method:
<div style="background-color:white; color:black
<pre> textBoxDeviceDetails.Text += getContent(<span style="color:#a31515 "name")[0] + <span style="color:#a31515 "rn";
textBoxDeviceDetails.Text += <span style="color:#a31515 "Number of pins:" + getContent(<span style="color:#a31515 "Pins", <span style="color:#a31515 "Count")[1] + <span style="color:#a31515 "rn";

[/code]

And this is my example XML file:
<div style="background-color:white; color:black
<pre><span style="color:blue <<span style="color:#a31515 device<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 name<span style="color:blue >NEC 555<span style="color:blue </<span style="color:#a31515 name<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Pins <span style="color:red Count<span style="color:blue =<span style="color:black "<span style="color:blue 2<span style="color:black "<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Pin<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 x<span style="color:blue >0<span style="color:blue </<span style="color:#a31515 x<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 y<span style="color:blue >0</y>[/code]
<pre> <span style="color:blue </<span style="color:#a31515 Pin<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 Pin<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 x<span style="color:blue >1<span style="color:blue </<span style="color:#a31515 x<span style="color:blue >
<span style="color:blue <<span style="color:#a31515 y<span style="color:blue >1</y>
<span style="color:blue </<span style="color:#a31515 Pin<span style="color:blue >
<span style="color:blue </<span style="color:#a31515 Pins<span style="color:blue >[/code]
<pre><span style="color:blue </<span style="color:#a31515 device<span style="color:blue >















[/code]

Output should be (imagine italic font below is in a textbox and bold is the things method supposed to return):
NEC 555
Number of pins: 2

I also want to know hot to read x and y from same siblings in Pins element please!<br/>
<br/>
Thanks.

View the full article
 
Back
Top