我有一个包含嵌套xml节点的xml文件。我需要遍历节点并将innertext保存到列表中。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
}
发布于 2020-07-11 01:37:22
我更喜欢@Yitzhak解决方案,但如果你想使用XmlDocument
,你可以尝试以下方法:
1-创建类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);
}
}
我希望这对你有帮助。
发布于 2020-07-11 01:11:00
最好使用LINQ to XML API。它在.Net框架中已经存在了十多年了。
提供的XML格式不正确。我得把它修好。
不清楚您想要的输出是什么。
我复制了@Sajid的类作为数据的占位符。
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; }
}
https://stackoverflow.com/questions/62838938
复制相似问题