簡體   English   中英

遍歷嵌套的 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,循環並獲取列表中的所有 object 類型。 我無法獲得嵌套節點。

以下是我嘗試過的代碼:

 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 框架中可用已有十多年了。

提供的 XML 格式不正確。 我不得不修復它。

目前尚不清楚您想要的 output 是什么。

我復制了@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);
    }
}

我希望你覺得這有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM