How to read attribute value from XmlNode in C#?
Try this:
string employeeName = chldNode.Attributes["Name"].Value;
Edit: As pointed out in the comments, this will throw an exception if the attribute doesn't exist. The safe way is:
var attribute = node.Attributes["Name"];
if (attribute != null){
string employeeName = attribute.Value;
// Process the value here
}
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;
How to Read XML node attributes?
If i'm understanding well your question, you should do something like that:
//...
using System.Linq;
using System.Xml.Linq;
//...
XDocument doc = XDocument.Load(@"C:\directory\file.xml");
IEnumerable<XElement> partElements = doc.Root.Elements("part");
foreach (XElement partElement in partElements)
{
// read attribute value
string keyName = partElement.Attribute("keyName")?.Value;
//...
// iterate through childnodes
foreach (XElement partChildElement in partElement.Elements())
{
// check the name
if (partChildElement.Name == "SpaceSep")
{
int value = (int)partChildElement; // casting from element to its [int] content
// do stuff for <SpaceSep> element
}
else if (partChildElement.Name == "text")
{
string text = (string)partChildElement; // casting from element to its [string] content
// do stuff for <text> element
}
// and so on for all possible node name
}
}
Read xml node attribute
You should use XmlNamespaceManager in your call to SelectSingleNode() since your XML does contain a namespace on it:
var doc = new XmlDocument();
doc.Load("example.xml");
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("anyname", "http://tempuri.org/zitem.xsd");
foreach (XmlNode node in doc.SelectNodes("//anyname:ITEM", ns))
{
Console.WriteLine(node.Attributes["id"].Value);
}
That's why you get no result.
The difference from my code to yours is that I am using //
so instead of starting at the root of a document, a double forward slash //
indicates to an XPath
evaluator to look anywhere in an XML document.
Here is my example.xml
as sample:
<root>
<items>
<ITEM id="1" name="bleh=" />
<ITEM id="2" name="bleh=" />
<ITEM id="3" name="bleh=" />
<ITEM id="4" name="bleh=" />
<ITEM id="5" name="bleh=" />
<ITEM id="6" name="bleh=" />
<ITEM id="7" name="bleh=" />
<ITEM id="8" name="bleh=" />
</items>
</root>
And here is how I am reading it:
var doc = new XmlDocument();
doc.Load("example.xml");
foreach (XmlNode node in doc.SelectNodes("//ITEM[@id]"))
{
Console.WriteLine(node.Attributes["id"].Value);
}
With single slash, the above XPath
would look like this:
/root/items/ITEM
I am also using [@id]
to ensure that the ITEM
element have an ID
attribute but that is not necessary if you know they all have an ID
attribute.
How to get xml node value based on attribute value c# , asp.net?
Try this
_productLinkType = _xmlDoc.SelectSingleNode(string.Format("//Products[..//ProductNumber = '{0}']", prodNumber));
or
var xDoc = XDocument.Load(_accountsXml);
var productLinkType = xDoc.XPathSelectElement(string.Format("//Products[..//ProductNumber = '{0}']", prodNumber)).Element("Product").Attribute("link").Value;
The closing tag in your example is Product it should be Products
How to get values of XML node filtering by attribute name in C#?
this may help you to solve your problem
XmlDocument doc = new XmlDocument();
doc.Load(_xmlFilePath);
XmlNodeList nodelist = doc.SelectNodes("//result[@value=45]");
for (int i = 0; i < nodelist.Count; i++)
{
double value = double.Parse(nodelist[i].InnerText);
Console.WriteLine("value : " + value);
}
Get child attributes of xml node c#
Your XPath is a little incorrect. For the purposes of testing, I hardcoded the values:
XmlNode messageNode = xmlDoc.SelectSingleNode("//Message[@name='Error']");
I think you will be able to easily substitute the hard-coded values for the inputs from the text boxes.
What this will do is search the whole Xml document for a Message
node that has an attribute called name
with the value of Error
. SelectSingleNode
will return the first occurrence if there are multiple matches. There is a SelectNodes
function that will return multiple values, if you need it.
The important bits are:
\\Message
- which instructs the XmlDocument to find theMessage
node@name
- instructs the XmlDocument to look for an attribute
When I ran this it found:
<Message dataItemId="Axis_02_InvDDone" timestamp="2018-06-25T20:20:40.4374489Z" name="Error" sequence="85998" nativeCode="208573">208573</Message>
I am not clear on which attribute you want to retrieve. This will retrieve the value of the dataItemId
attribute
Debug.Print(messageNode.Attributes["dataItemId"].InnerText);
Axis_02_InvDDone
To get the Text value of that node, ie 208573, use:
Debug.Print(messageNode.InnerText);
Related Topics
Are There Any Reasons to Use Private Properties in C#
Password Masking Console Application
How to "Multiply" a String (In C#)
Why Are C# 4 Optional Parameters Defined on Interface Not Enforced on Implementing Class
Setting/Getting the Class Properties by String Name
How to Populate a Dropdownlist from a Database
Test If String Is a Guid Without Throwing Exceptions
Restrict Access to a Specific Controller by Ip Address in ASP.NET MVC Beta
What Does the Word "Literal" Mean
How to View Msil/Cil Generated by C# Compiler? Why Is It Called Assembly
Why Isn't Array a Generic Type
How to Get Duplicate Items from a List Using Linq
How to Ensure a Form Displays on the "Additional" Monitor in a Dual Monitor Scenario
Is There a Lower Bound Function on a Sortedlist<K ,V>