简体   繁体   English

C# 从 xml 属性中获取值

[英]C# get values from xml attributes

How to get attribute "action" and "filename" values in a right way using C#?如何使用 C# 以正确的方式获取属性“action”和“filename”值?

XML: 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 as well as how to get values in foreach loops? C#:我无法获取属性值以及如何在 foreach 循环中获取值? 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 (var action in xmlActions)
         {
           //not working
         }

         foreach (var file in xmlFileNames)
         {
           //not working
         }

Your code example means alot to me.您的代码示例对我来说意义重大。 Thanks!谢谢!

You can use LINQ to XML .您可以使用LINQ to XML Following query returns strongly typed collection of items with Action and FileName properties:以下查询返回具有ActionFileName属性的强类型项集合:

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
}

GetElementsByTagName will find you only direct descendants. GetElementsByTagName会发现你唯一的直接后裔。 The argument is supposed to be just a tag name , not a whole path of elements.参数应该只是一个标签名称,而不是整个元素路径。

If you want to search across the document while supplying an XPath expression, use SelectNodes instead.如果您想在提供 XPath 表达式的同时搜索整个文档,请改用SelectNodes

For your document, it should look like this:对于您的文档,它应该如下所示:

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

You can achieve what you're asking with LINQ to XML :您可以使用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);
}

There are a bunch of problems with the code in the question:问题中的代码有一堆问题:
1. You are using an XPath in the GetElementsByTagName, just use the tag 1. 您在 GetElementsByTagName 中使用 XPath,只需使用标记
2. You are only getting the first XmlNode in the XmlNodeCollection by using [0] 2. 你只能通过使用 [0] 来获取 XmlNodeCollection 中的第一个 XmlNode
3. Since you only have one XmlNode, you are only getting a string result for getting the attribute, not a collection of strings, which you are then trying to enumerate through 3. 由于您只有一个 XmlNode,因此您只能获得用于获取属性的字符串结果,而不是您尝试枚举的字符串集合
4. Your foreach is broken, there is no type for the resulting object 4.你的foreach坏了,结果对象没有类型

Here is a snippet that would 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 of the actions and filenames in a collection before you act on them, you could just act on each action/filename in the for loop.或者,如果在对集合中的所有动作和文件名采取行动之前不需要它们,您可以只对 for 循环中的每个动作/文件名采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM