Getting Attribute Value of an Xml Document Using C#

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;

C# get values from xml attributes

You can use LINQ to XML. Following query returns strongly typed collection of items with Action and FileName properties:

var xdoc = XDocument.Load(@newFile);

var items = from i in xdoc.Descendants("Item")
select new {
Action = (string)i.Attribute("action"),
FileName = (string)i.Attribute("fileName")
};

foreach (var item in items)
{
// use item.Action or item.FileName
}

How get the attribute value of XML node in C#

You'll want to look at the ParentNode to get the attribute.

string residentType = xnList[0].ParentNode.Attributes["Type"].Value;

C# XDocument - Get value of an attribute in an XML file

Just select the Mod Name in your anonymous type:

 var elements = from r in document.Descendants("Mod")
select new
{
ModName = r.Attribute("Name").Value,
Author = r.Element("Author").Value,
Description = r.Element("Description").Value
};

foreach (var r in elements)
{
Console.WriteLine("MOD Name = " + r.ModName + Environment.NewLine + "AUTHOR = " + r.Author + Environment.NewLine + "DESCRIPTION = " + r.Description);
}

Getting attribute values of a tag in XML via C#

I think the function you are looking for might be XmlNode.SelectNodes.

var successes = doc.SelectNodes("/start/action/message[@result='success']").Count; // = 2
var failures = doc.SelectNodes("/start/action/message[@result='failure']").Count; // = 1

Get all the attribute values of nodes from an XML File

Use LINQ to XML:

XDocument doc = XDocument.Parse(xml);
var ids = doc.Descendants("Engagement").Attributes("id").Select(x => x.Value);

foreach (var id in ids)
Console.WriteLine(id);


Related Topics



Leave a reply



Submit