C # get values ​​from xml attributes

How to correctly get the values ​​of the attribute "action" and "file name" using C #?

XML:

<?xml version="1.0" encoding="utf-8" ?> <Config version="1.0.1.1" > <Items> <Item action="Create" filename="newtest.xml"/> <Item action="Update" filename="oldtest.xml"/> </Items> </Config> 

C #: I cannot get attribute values, and also how to get values ​​in foreach loops? How to solve this?

  var doc = new XmlDocument(); doc.Load(@newFile); var element = ((XmlElement)doc.GetElementsByTagName("Config/Items/Item")[0]); //null var xmlActions = element.GetAttribute("action"); //cannot get values var xmlFileNames= element.GetAttribute("filename"); //cannot get values foreach (action in xmlActions) { //not working } foreach (file in xmlFileNames) { //not working } 

Your sample code means a lot to me. Thanks!

+4
source share
4 answers

You can use LINQ to XML . The following query returns a strongly typed set of elements with the 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 } 
+7
source

GetElementsByTagName will only find you direct descendants. The argument should be just the tag name , not the whole path of the elements.

If you want to search a document when delivering an XPath expression, use SelectNodes .

For your document, it should look like this:

 var element = (XmlElement)doc.SelectNodes("/Config/Items/Item")[0]; 
+3
source

You can achieve what you specify with LINQ to XML :

 // For each element that is a child of your Items element that is named Item foreach (var item in XElement.Load("file.xml").Descendants("Items").Elements("Item")) { // If the element does not have any attributes if (!item.Attributes().Any()) { // Lets skip it continue; } // Obtain the value of your action attribute - Possible null reference exception here that should be handled var action = item.Attribute("action").Value; // Obtain the value of your filename attribute - Possible null reference exception here that should be handled var filename = item.Attribute("filename").Value; // Do something with your data Console.WriteLine("action: {0}, filename {1}", action, filename); } 
+2
source

There are a number of code issues in the question:
1. You are using XPath in GetElementsByTagName, just use the tag
2. You get only the first XmlNode in the XmlNodeCollection using [0]
3. Since you have only one XmlNode, you get only a string result to get the attribute, not a set of strings, which you then try to list through 4. Your foreach is broken, there is no type for the resulting object

Here is a snippet that will work:

 var doc = new XmlDocument(); doc.Load("test.xml"); var items = doc.GetElementsByTagName("Item"); var xmlActions = new string[items.Count]; var xmlFileNames = new string[items.Count]; for (int i = 0; i < items.Count; i++) { var xmlAttributeCollection = items[i].Attributes; if (xmlAttributeCollection != null) { var action = xmlAttributeCollection["action"]; xmlActions[i] = action.Value; var fileName = xmlAttributeCollection["filename"]; xmlFileNames[i] = fileName.Value; } } foreach (var action in xmlActions) { //working } foreach (var file in xmlFileNames) { //working } 

Or, if you don’t need all the actions and file names in the collection, before you act on them, you can simply act on each action / file name in a for loop.

+2
source

Source: https://habr.com/ru/post/1494964/


All Articles