Read Xml Attribute Using Xmldocument

Read XML Attribute using XmlDocument


XmlNodeList elemList = doc.GetElementsByTagName(...);
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["SuperString"].Value;
}

Reading XML data using XmlDocument with C# - Reading attribute data and splitting the results

It is better to use LINQ to XML API. It is available in the .Net Framework since 2007.

c#

void Main()
{
const string fileName = @"e:\Temp\FEHCDROME.xml";
XDocument xdoc = XDocument.Load(fileName);

XElement xelem = xdoc.Descendants("PointDescriptors").FirstOrDefault();

Console.WriteLine("PointDescriptors:");
Console.WriteLine("y[]: {0}", xelem.Attribute("y").Value);
Console.WriteLine("x[]: {0}", xelem.Attribute("x").Value);
Console.WriteLine("bfihost[]: {0}", xelem.Element("bfihost").Value);

XElement rp = xdoc.Descendants("ReturnPeriods").FirstOrDefault();
Console.WriteLine("{0}ReturnPeriods:", Environment.NewLine);
foreach (string s in rp.Value.Split(',').Select(sValue => sValue.Trim()).ToArray())
{
Console.WriteLine("{0} ", s);
}

Console.WriteLine("{0}Depths:", Environment.NewLine);
foreach (XElement dp in xdoc.Descendants("Depths"))
{
foreach (string s in dp.Value.Split(',').Select(sValue => sValue.Trim()).ToArray())
{
Console.WriteLine("{0} ", s);
}
}
}

Output

PointDescriptors:
y[]: 111111
x[]: 222222
bfihost[]: 0.386

ReturnPeriods:
1.3
1.58
2
...

Depths:
3.27693489525396
3.98688804941076
4.68688804941076
...
5.37693489525396
6.51484587430874
7.81484587430874
...
6.87693489525396
8.38688804941076
10.0017339237195
...

Edit xml file using xmldocument, search by attribute

This should work for you. Your main node already contains element Game - that's why you won't be able to find it in ChildNode. I corrected your main path not to include Game node. So now, you can find it in ChildNodes collection. I added some checks for Node.Name and Attribute name to be sure that it is Game node. It should work for you.

XmlNode node = xdoc["Data"]["Place"]["Date"];
int countvalues = 100;
for (int i = 0; i < countvalues; i++)
{
foreach (XmlNode childNode in node.ChildNodes)
{
if (childNode.Name == "Game" && childNode.Attributes["Name"].InnerText.Equals("Tennis"))
{
childNode["balance"].InnerText = xBal.Text;
}
}
}

Read XML Attribute using C#

You can simply use following code

 XmlNodeList elemList = doc.GetElementsByTagName("Your Element");
for (int i = 0; i < elemList.Count; i++)
{
string attrVal = elemList[i].Attributes["ID"].Value;
}

Demo: https://dotnetfiddle.net/5PpNPk

the above code is taken from here Read XML Attribute using XmlDocument

Getting attribute value of an XML Document using C#

I would try something like this:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<reply success=\"true\">More nodes go here</reply>");

XmlElement root = doc.DocumentElement;

string s = root.Attributes["success"].Value;

Getting nested XML attribute value using XmlDocument

[Once you have an XmlNode object, you can ask for its Attributes collection and get the one you want by name.

An example:

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlString);
XmlNode itemNode = doc.SelectSingleNode("/error/item[@name = 'PATH_INFO']");
if (itemNode != null)
{
XmlNode value = itemNode.SelectSingleNode("value");
String valueString = value.Attributes["string"].Value;
}

using xmldocument to read xml

The testcase element has no attributes. You should be looking to it's child nodes:

tc.name = node.SelectSingleNode("name").InnerText;
tc.date = node.SelectSingleNode("date").InnerText;
tc.sub = node.SelectSingleNode("subject").InnerText;

You might process all nodes like this:

var testCases = nodelist
.Cast<XmlNode>()
.Select(x => new CommonLib.TestCase()
{
name = x.SelectSingleNode("name").InnerText,
date = x.SelectSingleNode("date").InnerText,
sub = x.SelectSingleNode("subject").InnerText
})
.ToList();

get xml attribute name using xmldocument

An XmlNode has an Attributes collection. The items in this collection are XmlAttributes. XmlAttributes have Name and Value properties, among others.

The following is an example of looping through the attributes for a given node, and outputting the name and value of each attribute.

XmlNode node = GetNode();

foreach(XmlAttribute attribute in node.Attributes)
{
Console.WriteLine(
"Name: {0}, Value: {1}.",
attribute.Name,
attribute.Value);
}

Beware, from the docs for the XmlNode.Attributes:

If the node is of type XmlNodeType.Element, the attributes of the node
are returned. Otherwise, this property returns null.

Update

If you know that there are exactly two attributes, and you want both of their names at the same time, you can do something like the following:

string attributeOne = node.Attributes[0].Name;
string attributeTwo = node.Attributes[1].Name;

See http://msdn.microsoft.com/en-us/library/0ftsfa87.aspx



Related Topics



Leave a reply



Submit