pro*_*eek 4 c# python xml lxml
<Connections>
<Connection ID = "1" Source="1:0" Sink="4:0"/>
<Connection ID = "2" Source="2:0" Sink="4:1"/>
<Connection ID = "3" Source="2:0" Sink="5:0"/>
<Connection ID = "4" Source="3:0" Sink="5:1"/>
<Connection ID = "5" Source="4:0" Sink="6:0"/>
<Connection ID = "6" Source="5:0" Sink="7:0"/>
</Connections>
Run Code Online (Sandbox Code Playgroud)
当我需要从之前的 XML 代码中获取信息时,可以使用 Python 的 lxml,如下所示。
def getNodeList(self):
connection = self.doc.find('Connections')
cons = connection.find('Connection')
for con in cons.iter():
con.get("ID") # get attribute
...
Run Code Online (Sandbox Code Playgroud)
根据 dtb 的回答,我可以获得我需要的东西。
using System;
using System.Xml;
using System.Xml.Linq;
namespace HIR {
class Dummy {
static void Main(String[] argv) {
XDocument doc = XDocument.Load("test2.xml");
var connection = doc.Descendants("Connections"); // .First();
var cons = connection.Elements("Connection");
foreach (var con in cons)
{
var id = (string)con.Attribute("ID");
Console.WriteLine(id);
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
我必须删除“First()”以避免编译器错误。使用单声道,我可以运行以下命令来获取二进制文件。
dmcs /r:System.Xml.Linq main.cs
您想使用LINQ-to-XML:
void GetNodeList()
{
var connection = this.doc.Descendants("Connections").First();
var cons = connection.Elements("Connection");
foreach (var con in cons)
{
var id = (string)con.Attribute("ID");
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1517 次 |
| 最近记录: |