C# 如何从其他嵌套元素获取信息?
我正在努力使用LINQtoSQLXML。我在下面的例子中得到了一些好的结果。但是我遇到了一个问题,希望有人能帮助我C# 如何从其他嵌套元素获取信息?,c#,linq-to-xml,C#,Linq To Xml,我正在努力使用LINQtoSQLXML。我在下面的例子中得到了一些好的结果。但是我遇到了一个问题,希望有人能帮助我 <root> <MyItem Host="00155DFF045C" LastName="HOUSTON" FirstName="WHITNEY" > <TimeStamp ComputerTime="02/07/2011 - 21:41:53.715"> <Group Name="Group1">
<root>
<MyItem Host="00155DFF045C" LastName="HOUSTON" FirstName="WHITNEY" >
<TimeStamp ComputerTime="02/07/2011 - 21:41:53.715">
<Group Name="Group1">
<Variable ID="1001" Value="Happy" >
</Variable>
</Group>
<Group Name="Group2">
<Variable ID="2000" Value="Monday" >
</Variable>
<Variable ID="2001" Value="Tuesday" >
</Variable>
</Group>
<Group Name="Group3">
<Variable ID="3000" Value="January" >
</Variable>
<Variable ID="3001" Value="February" >
</Variable>
</Group>
<Group Name="Groupe4">
<Variable ID="4001" Value="108.000000" >
</Variable>
<Variable ID="4002" Value="800" >
</Variable>
</Group>
<Group Name="CustomGroup">
<Variable ID="1001000" Value="1.000000" >
</Variable>
</Group>
</TimeStamp>
</MyItem>
<MyItem>
...
...
</MyItem>
</root>
使用此代码,我可以正确地获取
DisplayName
的值。我想获取变量4001
的值,即值108.0000
如果您有对特定元素的引用,则可以使用元素()
或属性()
方法从其任何嵌套元素获取值。您可以使用Elements()
或Attributes()
获取这些元素的列表,以对它们执行LINQ查询。假设我们有根元素的引用,您可以获得以下信息:
XElement root = ...;
XElement myItem = root.Element("MyItem"); // get the first "MyItem" element
string Host = myItem.Attribute("Host").Value; // get value of "Host" attribute
string LastName = myItem.Attribute("LastName").Value; // get value of "LastName" attribute
string FirstName = myItem.Attribute("FirstName").Value; // get value of "FirstName" attribute
// find "Groupe4"
XElement Groupe4 = (from g in myItem.Element("TimeStamp")
.Elements("Group")
where g.Attribute("Name").Value == "Groupe4"
select g) // only one element should be found
.Single(); // assign that element to variable Groupe4
// Get Value of Variable with ID = 4001
double Value4001 = (from v in Groupe4.Elements("Variable") // of all Variable elements
where (int)v.Attribute("ID") == 4001 // choose elements where ID = 4001
select (double)v.Attribute("Value")) // select the Value
.Single(); // assign that value to variable Value 4001
因此,要将其应用于查询,可以执行以下操作:
XElement xmlTweets = XElement.Parse(e.Result);
var id = 4001; // the ID to find
var toto = from tweet in xmlTweets.Descendants("MyItem")
.Take(10)
orderby (DateTime)tweet.Element("TimeStamp")
.Attribute("ComputerTime") descending
select new History {
DisplayName = String.Format("{0} {1}",
tweet.Attribute("PatientFirstName").Value,
tweet.Attribute("PatientLastName").Value),
// find the Variable with matching ID and get its Value as a double
Value = (from v in tweet.Descendants("Variable")
where (int)v.Attribute("ID") == id
select (double)v.Attribute("Value"))
.Single()
};
如果有对特定元素的引用,则可以使用
element()
或Attribute()
方法从其任何嵌套元素获取值。您可以使用Elements()
或Attributes()
获取这些元素的列表,以对它们执行LINQ查询。假设我们有根元素的引用,您可以获得以下信息:
XElement root = ...;
XElement myItem = root.Element("MyItem"); // get the first "MyItem" element
string Host = myItem.Attribute("Host").Value; // get value of "Host" attribute
string LastName = myItem.Attribute("LastName").Value; // get value of "LastName" attribute
string FirstName = myItem.Attribute("FirstName").Value; // get value of "FirstName" attribute
// find "Groupe4"
XElement Groupe4 = (from g in myItem.Element("TimeStamp")
.Elements("Group")
where g.Attribute("Name").Value == "Groupe4"
select g) // only one element should be found
.Single(); // assign that element to variable Groupe4
// Get Value of Variable with ID = 4001
double Value4001 = (from v in Groupe4.Elements("Variable") // of all Variable elements
where (int)v.Attribute("ID") == 4001 // choose elements where ID = 4001
select (double)v.Attribute("Value")) // select the Value
.Single(); // assign that value to variable Value 4001
因此,要将其应用于查询,可以执行以下操作:
XElement xmlTweets = XElement.Parse(e.Result);
var id = 4001; // the ID to find
var toto = from tweet in xmlTweets.Descendants("MyItem")
.Take(10)
orderby (DateTime)tweet.Element("TimeStamp")
.Attribute("ComputerTime") descending
select new History {
DisplayName = String.Format("{0} {1}",
tweet.Attribute("PatientFirstName").Value,
tweet.Attribute("PatientLastName").Value),
// find the Variable with matching ID and get its Value as a double
Value = (from v in tweet.Descendants("Variable")
where (int)v.Attribute("ID") == id
select (double)v.Attribute("Value"))
.Single()
};
一旦您拥有名为
myItemElement
的和XElement
变量
myItemElement
.Element("TimeStamp")
.Elements("Group")
.FirstOrDefault(x => x.Attribute("Name") != null && x.Attribute("Name").Value == "Groupe4")
.Elements("Variable")
.FirstOrDefault(x => x.Attribute("ID") != null && x.Attribute("ID").Value == "4001")
.Attribute("Value").Value;
一旦您拥有名为
myItemElement
的和XElement
变量
myItemElement
.Element("TimeStamp")
.Elements("Group")
.FirstOrDefault(x => x.Attribute("Name") != null && x.Attribute("Name").Value == "Groupe4")
.Elements("Variable")
.FirstOrDefault(x => x.Attribute("ID") != null && x.Attribute("ID").Value == "4001")
.Attribute("Value").Value;
到目前为止,你有没有写下任何代码?这将使你知道如何做,更容易做到。嗨,杰夫,我只是用我当前的代码更新了我的初始帖子。希望你能帮助我:)如果你有一些源代码你想格式化,只要粘贴它和缩进四个空格。它会很好地展示给你。只是要确保在文字后面调整它,使其可读。:)Thx这么多,它只是与你给我的工作!到目前为止,你有没有写下任何代码?这将使你知道如何做,更容易做到。嗨,杰夫,我只是用我当前的代码更新了我的初始帖子。希望你能帮助我:)如果你有一些源代码你想格式化,只要粘贴它和缩进四个空格。它会很好地展示给你。只是要确保在文字后面调整它,使其可读。:)Thx这么多,它只是与你给我的工作!嗨,聪明的穴居人,你的解决方案也很有效!很多thx给你们两个。嗨,聪明的穴居人,你们的解决方案也很有效!祝你们俩多谢。