XML question

  • Thread starter Thread starter moonstonelane
  • Start date Start date
M

moonstonelane

Hi all,

I just picked up XML and trying to figure out on CSharp.
Would like some pointers here.

E.g of my XML file

<configfile avi-in-btw="1" gif-in-btw="1">
<keyframecount num="2">
<backgroundfile
num="1">C:\work\dev\JCacaniServer\BackgroundDir\disney_bg1.jpg</backgroundfile>
<backgroundfile
num="2">C:\work\dev\JCacaniServer\BackgroundDir\disney_bg2.jpg</backgroundfile>
</keyframecount>
<textcount num="0"/>
</configfile>

XmlNodeList elements = xmlDocument.SelectNodes("//configfile");
foreach (XmlElement element in elements)
{
Console.WriteLine("avi-in-btw: " +
element.GetAttribute("avi-in-btw"));
Console.WriteLine("gif-in-btw: " +
element.GetAttribute("gif-in-btw"));
}

Since there would only be one element <configfile>, i would like to
just get the single element <configfile> and get the attribute in it.
How do i go about doing it?

elements =
xmlDocument.SelectNodes("//configfile/keyframecount/backgroundfile");
foreach (XmlElement element in elements)
{
Console.Write("num: " + element.GetAttribute("num") +"
");
if (element.Value == null)
Console.WriteLine("No background file");
else
Console.WriteLine(element.Value);
}

i would like to get the text value for the element <backgroundfile>,
but seems like element.Value does not work.

Appreciate any advices :)
 
Hi all,

I just picked up XML and trying to figure out on CSharp.
Would like some pointers here.

E.g of my XML file

<configfile avi-in-btw="1" gif-in-btw="1">
<keyframecount num="2">
<backgroundfile
num="1">C:\work\dev\JCacaniServer\BackgroundDir\disney_bg1.jpg</backgroundfile>
<backgroundfile
num="2">C:\work\dev\JCacaniServer\BackgroundDir\disney_bg2.jpg</backgroundfile>
</keyframecount>
<textcount num="0"/>
</configfile>

XmlNodeList elements = xmlDocument.SelectNodes("//configfile");
foreach (XmlElement element in elements)
{
Console.WriteLine("avi-in-btw: " +
element.GetAttribute("avi-in-btw"));
Console.WriteLine("gif-in-btw: " +
element.GetAttribute("gif-in-btw"));
}

Since there would only be one element <configfile>, i would like to
just get the single element <configfile> and get the attribute in it.
How do i go about doing it?

XmlElement configfile =
(XmlElement)xmlDocument.SelectSingleNode("/configfile");
Console.WriteLine("avi-in-btw: " + configfile.GetAttribute("avi-in-btw"));

elements =
xmlDocument.SelectNodes("//configfile/keyframecount/backgroundfile");
foreach (XmlElement element in elements)
{
Console.Write("num: " + element.GetAttribute("num") +"
");
if (element.Value == null)
Console.WriteLine("No background file");
else
Console.WriteLine(element.Value);
}

i would like to get the text value for the element <backgroundfile>,
but seems like element.Value does not work.

Elements don't have a Value, only Attributes and Text Nodes. Technically
there is a text node under the Element that has the value you're looking
for, or you can access the XmlElement.InnerText.

David
 
David said:
Elements don't have a Value, only Attributes and Text Nodes. Technically
there is a text node under the Element that has the value you're looking
for, or you can access the XmlElement.InnerText.

Appreciate that advice

How about just getting a single element instead of a XmlNodeList? Since
i know there is only one such element there is no point getting a
XmlNodeList.
 
How about just getting a single element instead of a XmlNodeList? Since
i know there is only one such element there is no point getting a
XmlNodeList.

Using SelectSingleNode instead of SelectNodes gives you a single node
(or null if none is found).
 
Back
Top