我想从使用LINQ表达式的scxml文件中的“状态”和“转换”中获取属性。如何从xml/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" 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>
下面是我在做什么:
var scxml = XDocument.Load(@"c:\test_scmxl.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>
我试图获得像这样的所有“状态”:
foreach (var s in scxml.Descendants("state"))
{
Console.WriteLine(s.FirstAttribute);
}
而当我打印它看看我是否得到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");
}
这表明我:
<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.Descendants("state"))
{
Console.WriteLine(state.Attribute("id"));
}
编辑:下面的代码也不起作用。控制台警告无效可能性(可抑制)。没有东西会回来。
foreach (var state in scxml.Root.Descendants("state"))
{
Console.WriteLine(state.Attribute("id"));
}
对不起,我没有设法找出问题的症结所在。 –
@OndrejJanacek,“IDeveloper”有一个很好的解决方案。只是为了你知道。 =) – Th3B0Y
谢谢,我明白这一点:)我不会来这个解决方案。 –