简体   繁体   English

如何从其他嵌套元素获取信息?

[英]How do I get information from other nested elements?

I am struggling with using LINQ to SQL XML. 我正在努力使用LINQ to SQL XML。 I'm following examples and getting some good results. 我正在跟踪示例,并获得了一些良好的结果。 However I ran into a problem and hope that someone can help me. 但是我遇到了一个问题,希望有人可以帮助我。

<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>

I correctly get info like {Host="00155DFF045C" LastName="HOUSTON" FirstName="WHITNEY} thanks to the tutorial. 由于本教程,我正确地获得了诸如{Host="00155DFF045C" LastName="HOUSTON" FirstName="WHITNEY}这样的信息。

But I need to get some more information. 但是我需要获得更多信息。 How can I retrieve Value of the Variable element with ID="4001" which belongs to Groupe4 ? 如何取回Value用变量元件的ID="4001" ,其属于Groupe4

My current code is: 我当前的代码是:

XElement xmlTweets = XElement.Parse(e.Result);
var toto = from tweet in xmlTweets.Descendants("MyItem").Take(10)
           orderby tweet.Element("TimeStamp").Attribute("ComputerTime").Value descending
           select new History {
               DisplayName = tweet.Attribute("PatientFirstName").Value + " " +
                             tweet.Attribute("PatientLastName").Value
           };

With this code, I can correctly get value for DisplayName . 使用此代码,我可以正确获取DisplayName值。 I would like to get value of variable 4001 , ie, value 108.0000 . 我想获取变量4001值,即值108.0000

If you have a reference to a particular element, you can get values from any of its nested elements using the Element() or Attribute() methods. 如果您有对特定元素的引用,则可以使用Element()Attribute()方法从其任何嵌套元素中获取值。 You can get a list of these using Elements() or Attributes() to perform LINQ queries on them. 您可以使用Elements()Attributes()获得对它们的列表,以对它们执行LINQ查询。 Assuming we have a reference to the root element, you can get the following information: 假设我们有一个对root元素的引用,您可以获得以下信息:

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

So to apply that to your query, you can do something like this: 因此,要将其应用于您的查询,您可以执行以下操作:

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()
           };

Once you have and XElement variable named myItemElement , 一旦有了XElement变量myItemElement

     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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM