C# 如何从xml/scxml获取属性
我试图使用LINQ表达式从scxml文件中的“状态”和“转换”中获取属性 以下是scxml文件:C# 如何从xml/scxml获取属性,c#,xml,linq,xml-parsing,linq-to-xml,C#,Xml,Linq,Xml Parsing,Linq To Xml,我试图使用LINQ表达式从scxml文件中的“状态”和“转换”中获取属性 以下是scxml文件: <?xml version="1.0" encoding="utf-8"?> <scxml xmlns:musthave="http://musthave.com/scxml/1.0" version="1.0" initial="Start" xmlns="http://www.w3.org/2005/07/scxml"> <state id="abc" mu
<?xml version="1.0" encoding="utf-8"?>
<scxml xmlns:musthave="http://musthave.com/scxml/1.0" version="1.0" initial="Start" xmlns="http://www.w3.org/2005/07/scxml">
<state id="abc" musthave:displaystate="abcd" musthave:attribute1="None" musthave:attribute2="None">
<transition attribute3="blabla" attribute4="blabla" xmlns=""/>
</state>
<state id="bla" musthave:displaystate="ababab" musthave:attribute2="View" musthave:attribute1="View"/>
</scxml>
如果我在控制台上打印,它会显示:
<scxml xmlns:musthave="http://musthave.com/scxml/1.0" version="1.0" initial="Start" xmlns="http://www.w3.org/2005/07/scxml">
<state id="abc" musthave:displaystate="abcd" musthave:attribute1="None" musthave:attribute2="None">
<transition attribute3="blabla" attribute4="blabla" xmlns=""/>
</state>
<state id="bla" musthave:displaystate="ababab" musthave:attribute2="View" musthave:attribute1="View"/>
</scxml>
<state id="abc" musthave:displaystate="abcd" musthave:attribute1="None" musthave:attribute2="None" xmlns:musthave="http://musthave.com/scxml/1.0" xmlns="http://www.w3.org/2005/07/scxml">
<transition attribute3="blabla" attribute4="blabla" xmlns="" />
</state>
<state id="bla" musthave:displaystate="" musthave:attribute2="View" musthave:attribute1="View" xmlns:musthave="http://musthave.com/scxml/1.0"
xmlns="http://www.w3.org/2005/07/scxml" />
在本例中,当我打印它以查看是否得到id=“abc”时,它不会返回任何内容
尽管如此,如果我运行代码:
foreach (var xNode in scxml.Elements().Select(element => (from test in element.Nodes() select test)).SelectMany(a => a))
{
Console.WriteLine(xNode);
Console.WriteLine("\n\n\n");
}
它告诉我:
<scxml xmlns:musthave="http://musthave.com/scxml/1.0" version="1.0" initial="Start" xmlns="http://www.w3.org/2005/07/scxml">
<state id="abc" musthave:displaystate="abcd" musthave:attribute1="None" musthave:attribute2="None">
<transition attribute3="blabla" attribute4="blabla" xmlns=""/>
</state>
<state id="bla" musthave:displaystate="ababab" musthave:attribute2="View" musthave:attribute1="View"/>
</scxml>
<state id="abc" musthave:displaystate="abcd" musthave:attribute1="None" musthave:attribute2="None" xmlns:musthave="http://musthave.com/scxml/1.0" xmlns="http://www.w3.org/2005/07/scxml">
<transition attribute3="blabla" attribute4="blabla" xmlns="" />
</state>
<state id="bla" musthave:displaystate="" musthave:attribute2="View" musthave:attribute1="View" xmlns:musthave="http://musthave.com/scxml/1.0"
xmlns="http://www.w3.org/2005/07/scxml" />
编辑:以下代码也不起作用。控制台警告可能为空(可抑制)。什么也不归还
foreach (var state in scxml.Root.Descendants("state"))
{
Console.WriteLine(state.Attribute("id"));
}
在
scxml
标记中有一个名称空间,因此您需要将其与内部标记一起使用以访问它们。以下是您需要的代码:
XDocument xdoc = XDocument.Load(path_to_xml);
XNamespace ns = "http://www.w3.org/2005/07/scxml";
foreach (var state in xdoc.Descendants(ns + "state"))
{
Console.WriteLine(state.Attribute("id").Value);
}
很抱歉,我没有找到问题所在。@OndrejJanacek,“IdeDeveloper”有一个很好的解决方案。只是想让你知道。=)谢谢,我很感激:)我不会想到这个解决方案。
XDocument xdoc = XDocument.Load(path_to_xml);
XNamespace ns = "http://www.w3.org/2005/07/scxml";
foreach (var state in xdoc.Descendants(ns + "state"))
{
Console.WriteLine(state.Attribute("id").Value);
}