C# C从xml属性中获取值

C# C从xml属性中获取值,c#,xml,xmldocument,C#,Xml,Xmldocument,如何使用C以正确的方式获取属性操作和文件名值 XML: 你的代码示例对我来说意义重大。谢谢 将只找到您的直系后代。参数应该只是一个标记名,而不是元素的整个路径 如果要在提供XPath表达式的同时搜索整个文档,请改用 对于您的文档,它应该如下所示: var element = (XmlElement)doc.SelectNodes("/Config/Items/Item")[0]; 您可以通过以下方式实现您的要求: 你可以用。以下查询返回具有操作和文件名属性的强类型项集合: var xdoc =

如何使用C以正确的方式获取属性操作和文件名值

XML:

你的代码示例对我来说意义重大。谢谢

将只找到您的直系后代。参数应该只是一个标记名,而不是元素的整个路径

如果要在提供XPath表达式的同时搜索整个文档,请改用

对于您的文档,它应该如下所示:

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

您可以通过以下方式实现您的要求:

你可以用。以下查询返回具有操作和文件名属性的强类型项集合:

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
}

问题中的代码有很多问题: 1.如果在GetElementsByTagName中使用XPath,只需使用标记即可 2.使用[0]只能获取XmlNodeCollection中的第一个XmlNode 3.因为您只有一个XmlNode,所以您只能获得获取属性的字符串结果,而不是字符串集合,然后您将尝试枚举这些字符串 4.您的foreach已损坏,结果对象没有类型

下面是一个可以使用的代码片段:

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
}

或者,如果在对集合执行操作之前不需要集合中的所有操作和文件名,可以只对for循环中的每个操作/文件名执行操作。

我尝试过SelectNode,但不确定如何在代码中使用它。你能给我一个示例代码吗?@user235973457:我已经添加了示例调用;我现在无法尝试,但我希望名称空间不会有问题。如果您遇到任何错误,请包含确切的错误消息,我或这里的其他人稍后将重试。@user235973457:您的意思是XmlActions只包含一个元素吗?这是预期的,因为GetAttribute将只返回一个属性节点。如果您确实希望获取其操作属性为…(的所有节点),请迭代元素集合中SelectNodes的结果,并从每个元素中检索操作和文件属性。您可能需要查看。它使使用XML变得容易得多。var items=for i in xdoc.genderantsitem select new{Action=stringi.Attributeaction,FileName=stringi.AttributefileName};这不是正确的代码grammar@user235973457对不起,打字错误。它应该是从而不是为
// 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);
}
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
}
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
}