遍历嵌套的 XML 节点并将其保存到 asp.net c# 中的列表

Iterate through nested XML nodes and save it to a list in asp.net c#

我有一个包含 xml 个嵌套节点的 xml 文件。我需要遍历节点并将内部文本保存到列表中。 xml 格式如下:

<xml>
<record>
</record>
<Actions>
<Action Name= "name1">
<screen>
<subAction></SubAction>
</screen>
</Action>
<Action Name = "name2">
<screen Description= "info">
<subAction>
<object>
<name> user </name>
<type> string </type>
<variable> ram </variable>
</object>
<object>
<name> user1 </name>
<type> string1 </type>
<variable> ram1 </variable>
</object>
</subAction>
</screen>
<Screen Description= "info1">
<subAction>
<object>
</object>
</subAction>
</Screen>....goes on
</Action>
</Actions>
</xml>

我需要检查是否 Action == name2,遍历并获取列表中的所有对象类型。我无法获取嵌套节点。

下面是我试过的代码:

 XmlNodeList NodesPro = xmlDoc.SelectNodes("/xml/Actions/Action");
 foreach (XmlNode pronode in NodesPro)
                {
                    bool flag = false;
                    if (pronode.Attributes["Name"].Value == "name2")
                    { 
//Dont know how to proceed. Please help 
}

最好用LINQ来XML API。它在 .Net Framework 中可用已有十多年了。

提供的 XML 格式不正确。我必须修复它。

不清楚你想要的输出是什么。

我复制了@Sajid 的 class 作为数据的占位符。

c#

void Main()
{
    XDocument xsdoc = XDocument.Parse(@"<xml>
            <record>
            </record>
            <Actions>
                <Action Name='name1'>
                    <screen>
                        <subAction></subAction>
                    </screen>
                </Action>
                <Action Name='name2'>
                    <screen Description='info'>
                        <subAction>
                            <object>
                                <name>user</name>
                                <type>string</type>
                                <variable>ram</variable>
                            </object>
                            <object>
                                <name>user1</name>
                                <type>string1</type>
                                <variable>ram1</variable>
                            </object>
                        </subAction>
                    </screen>
                    <Screen Description='info1'>
                        <subAction>
                            <object>
                            </object>
                        </subAction>
                    </Screen>....goes on</Action>
            </Actions>
        </xml>");

    List<ActionObject> objects3 = new List<ActionObject>();

    foreach (var el in xsdoc.Descendants("Action")
        .Where(x => x.Attribute("Name").Value.Equals("name2")))
    {
        objects3 = el.Descendants("object")
            .Select(p => new ActionObject()
            {
                Name = p.Element("name")?.Value,
                Type = p.Element("type")?.Value,
                Variable = p.Element("variable")?.Value
            }).ToList();
    }
}

public class ActionObject
{
    public string Name { get; set; }
    public string Type { get; set; }
    public string Variable { get; set; }
}

我更喜欢@Yitzhak 的解决方案,但如果你想使用 XmlDocument,你可以尝试以下方法:

1 - 创建 class ActionObject:

public class ActionObject
{
    public string Name { get; set; }

    public string Type { get; set; }

    public string Variable { get; set; }
}

2 - Xml

string xml2 = @"
            <xml>
                <record>
                </record>
                <Actions>
                    <Action Name= 'name1'>
                    <screen></screen>
                    <subAction></subAction>
                    </Action>
                    <Action Name = 'name2'>
                        <screen Description= 'info'>
                            <subAction>
                                <object>
                                    <name> user </name>
                                    <type> string </type>
                                    <variable> ram </variable>
                                </object>
                                <object>
                                    <name> user1 </name>
                                    <type> string1 </type>
                                    <variable> ram1 </variable>
                                </object>
                            </subAction>
                        </screen>
                        <Screen Description= 'info1'>
                            <subAction>
                                <object></object>
                            </subAction>
                        </Screen>
                    </Action>
                </Actions>
            </xml>";

3 - 从 xml:

获取对象的代码
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xml2);
List<ActionObject> objects = new List<ActionObject>();

XmlNodeList actions = xmlDocument.DocumentElement.SelectNodes("/xml/Actions/Action");
foreach (XmlElement actionNode in actions)
{
    if (actionNode.Attributes["Name"].Value != "name2")
        continue;

    foreach(XmlNode objectNode in actionNode.SelectNodes("./screen/subAction/object"))
    {
        ActionObject actionObject = new ActionObject
        {
            Name = objectNode.SelectSingleNode("name").InnerText.Trim(),
            Type = objectNode.SelectSingleNode("type").InnerText.Trim(),
            Variable = objectNode.SelectSingleNode("variable").InnerText.Trim(),
        };

        objects.Add(actionObject);
    }
}

希望对您有所帮助。