I am trying to get a specific block of a XML, I am using function load to take all my XML file and it works fine. When I am debugging i can see all the file. The problem is about when I am trying to get a specific value of xml.
我正在尝试获得一个特定的XML块,我正在使用函数load来获取我所有的XML文件,它工作得很好。当我调试时,我可以看到所有的文件。问题是当我试图获取xml的特定值时。
I am using XElement of LINQ library. So, here is an example of my xml file:
我正在使用LINQ库的XElement。下面是我的xml文件示例:
-<Mission xmlns:z="http:..." xmlns:i="http:..." xmlns="http:..." z:Id="1">
+<ActiveBullsEye z:Id="2" i:type="BullsEye">
-<ActiveFlightPlan z:Id="7" i:type="FlightPlan">
<AIRTAC z:Id="8"/>
<Active>false</Active>
-<Aircraft z:Id="9" i:type="a:Tanker" xmlns:a="http:...">
-<ACColor xmlns:b="http:...">
<b:A>255</b:A>
<b:B>169</b:B>
<b:G>169</b:G>
<b:R>169</b:R>
<b:ScA>1</b:ScA>
<b:ScB>0.396755248</b:ScB>
<b:ScG>0.396755248</b:ScG>
<b:ScR>0.396755248</b:ScR>
I need to access block (ACColor) and then do a for statement to get all of these values. But I am trying something like this and not function for me:
我需要访问block (ACColor),然后执行一个for语句来获取所有这些值。但我正在尝试这样的东西,而不是为我自己:
XElement xdocument = XElement.load(filepath) //This works
XElement missionBlock = xdocument.Element("Mission") //(ERROR) This not get Mission tag
foreach( XElement acColor in missionBlock.Elements("ACColor") ) { // (ERROR) Not found ACColor
...
}
Could you help me to access to all values of ACColor node?
你能帮我访问ACColor节点的所有值吗?
3 个解决方案
#1
2
XElement xDocument = XElement.load(filepath); // This works
var ns = xDocument.GetDefaultNamespace();
in missionBlock
.Elements(ns + "ActiveBullsEye")
and to get at for instance the value of z:Id
:
比如得到z:Id的值:
var z = xml.GetNamespaceOfPrefix("z");
var id = (string)xElement.Attribute(z + "Id");
Do not use var when you create a namespace from a string:
当您从字符串创建名称空间时,不要使用var:
var ns1 = "http://something"; // ns1 is a string
XNamespace ns2 = "http://something"; // ns2 is a namespace
And you really need a namespace z for z + "Id"
to work.
你真的需要一个命名空间z来表示z +“Id”的工作。
#2
2
Try this:
试试这个:
class Program
{
static void Main(string[] args)
{
var xdocument = XDocument.Load(@"c:\temp\sta01\test.xml");
var xmlns = "http:...";
var missionBlock = xdocument.Root;
foreach (
var acColor
in missionBlock
.Elements(XName.Get("ActiveBullsEye", xmlns))
.Elements(XName.Get("ActiveFlightPlan", xmlns))
.Elements(XName.Get("Aircraft", xmlns))
.Elements(XName.Get("ACColor", xmlns)))
{
var channel = acColor.Element(XName.Get("A", xmlns));
Console.WriteLine($"A: {channel.Value}");
}
Console.ReadLine();
}
}
It works with this doc:
它与这个医生合作:
<Mission xmlns:z="http:..." xmlns:i="http:..." xmlns="http:..." z:Id="1">
<ActiveBullsEye z:Id="2" i:type="BullsEye">
<ActiveFlightPlan z:Id="7" i:type="FlightPlan">
<AIRTAC z:Id="8"/>
<Active>false</Active>
<Aircraft z:Id="9" i:type="a:Tanker" xmlns:a="http:...">
<ACColor xmlns:b="http:...">
<b:A>255</b:A>
<b:B>169</b:B>
<b:G>169</b:G>
<b:R>169</b:R>
<b:ScA>1</b:ScA>
<b:ScB>0.396755248</b:ScB>
<b:ScG>0.396755248</b:ScG>
<b:ScR>0.396755248</b:ScR>
</ACColor>
</Aircraft>
</ActiveFlightPlan>
</ActiveBullsEye>
</Mission>
#3
1
Use a dictionary like code below :
使用字典类似代码如下:
XDocument doc = XDocument.Load(FILENAME);
XElement color = doc.Descendants().Where(x => x.Name.LocalName == "ACColor").FirstOrDefault();
Dictionary<string, string> dict = color.Elements().GroupBy(x => x.Name.LocalName, y => (string)y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
#1
2
XElement xDocument = XElement.load(filepath); // This works
var ns = xDocument.GetDefaultNamespace();
in missionBlock
.Elements(ns + "ActiveBullsEye")
and to get at for instance the value of z:Id
:
比如得到z:Id的值:
var z = xml.GetNamespaceOfPrefix("z");
var id = (string)xElement.Attribute(z + "Id");
Do not use var when you create a namespace from a string:
当您从字符串创建名称空间时,不要使用var:
var ns1 = "http://something"; // ns1 is a string
XNamespace ns2 = "http://something"; // ns2 is a namespace
And you really need a namespace z for z + "Id"
to work.
你真的需要一个命名空间z来表示z +“Id”的工作。
#2
2
Try this:
试试这个:
class Program
{
static void Main(string[] args)
{
var xdocument = XDocument.Load(@"c:\temp\sta01\test.xml");
var xmlns = "http:...";
var missionBlock = xdocument.Root;
foreach (
var acColor
in missionBlock
.Elements(XName.Get("ActiveBullsEye", xmlns))
.Elements(XName.Get("ActiveFlightPlan", xmlns))
.Elements(XName.Get("Aircraft", xmlns))
.Elements(XName.Get("ACColor", xmlns)))
{
var channel = acColor.Element(XName.Get("A", xmlns));
Console.WriteLine($"A: {channel.Value}");
}
Console.ReadLine();
}
}
It works with this doc:
它与这个医生合作:
<Mission xmlns:z="http:..." xmlns:i="http:..." xmlns="http:..." z:Id="1">
<ActiveBullsEye z:Id="2" i:type="BullsEye">
<ActiveFlightPlan z:Id="7" i:type="FlightPlan">
<AIRTAC z:Id="8"/>
<Active>false</Active>
<Aircraft z:Id="9" i:type="a:Tanker" xmlns:a="http:...">
<ACColor xmlns:b="http:...">
<b:A>255</b:A>
<b:B>169</b:B>
<b:G>169</b:G>
<b:R>169</b:R>
<b:ScA>1</b:ScA>
<b:ScB>0.396755248</b:ScB>
<b:ScG>0.396755248</b:ScG>
<b:ScR>0.396755248</b:ScR>
</ACColor>
</Aircraft>
</ActiveFlightPlan>
</ActiveBullsEye>
</Mission>
#3
1
Use a dictionary like code below :
使用字典类似代码如下:
XDocument doc = XDocument.Load(FILENAME);
XElement color = doc.Descendants().Where(x => x.Name.LocalName == "ACColor").FirstOrDefault();
Dictionary<string, string> dict = color.Elements().GroupBy(x => x.Name.LocalName, y => (string)y)
.ToDictionary(x => x.Key, y => y.FirstOrDefault());