简体   繁体   English

Linq to XML查询嵌套元素

[英]Linq to XML query nested elements

Hi I have been working on this for two days and I don't know why it doesn't work. 嗨,我已经为此工作了两天,但我不知道为什么它不起作用。 I have the following XML 我有以下XML

Data = new XDocument(
             new XDeclaration("1.0", "utf-8", "yes"),
             new XComment("UserData"),
             new XElement("Users",
                 new XElement("user",
                        new XAttribute("name", "Bugs Bunny"),
                        new XAttribute("userID", "bbunny"),
                        new XAttribute("usertype", "Customer"),
                            new XElement("account",
                                new XAttribute("accounttype", "checking"),
                                new XAttribute("actbal", 1000),
                                new XAttribute("actbaldate", "1/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "1/1/2013"),
                                        new XAttribute("tranAmount", 1000),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 1000)))),

                new XElement("user",
                        new XAttribute("name", "Tasmanian Devil"),
                        new XAttribute("userID", "tdevil"),
                        new XAttribute("usertype", "Customer"),
                            new XElement("account",
                                new XAttribute("accounttype", "checking"),
                                new XAttribute("actbal", -20),
                                new XAttribute("actbaldate", "5/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "1/1/2013"),
                                        new XAttribute("tranAmount", 1000),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 1000)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "2/1/2012"),
                                        new XAttribute("tranAmount", 900),
                                        new XAttribute("trantype", "withdrawal"),
                                        new XAttribute("tranBalance", 100)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "5/1/2012"),
                                        new XAttribute("tranAmount", 120),
                                        new XAttribute("trantype", "withdrawal"),
                                        new XAttribute("tranBalance", -20)))),

                new XElement("user",
                        new XAttribute("name", "Sam Sheepdog"),
                        new XAttribute("userID", "ssheepdog"),
                        new XAttribute("usertype", "Customer"),
                        new XElement("account",
                                new XAttribute("accounttype", "checking"),
                                new XAttribute("actbal", 1000),
                                new XAttribute("actbaldate", "5/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "1/1/2013"),
                                        new XAttribute("tranAmount", 1000),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 1000)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "2/1/2012"),
                                        new XAttribute("tranAmount", 500),
                                        new XAttribute("trantype", "transfer"),
                                        new XAttribute("tranBalance", 500)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "5/1/2012"),
                                        new XAttribute("tranAmount", 500),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 1000))),
                       new XElement("account",
                                new XAttribute("accounttype", "savings"),
                                new XAttribute("actbal", 500),
                                new XAttribute("actbaldate", "5/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "5/1/2012"),
                                        new XAttribute("tranAmount", 500),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 500)))),


                 new XElement("user",
                        new XAttribute("name", "Elmer J. Fudd"),
                        new XAttribute("userID", "efudd"),
                        new XAttribute("usertype", "Customer"),
                        new XElement("account",
                                new XAttribute("accounttype", "checking"),
                                new XAttribute("actbal", 100000),
                                new XAttribute("actbaldate", "5/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "1/1/2013"),
                                        new XAttribute("tranAmount", 100000),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 100000)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "2/1/2012"),
                                        new XAttribute("tranAmount", 50000),
                                        new XAttribute("trantype", "transfer"),
                                        new XAttribute("tranBalance", 50000)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "5/1/2012"),
                                        new XAttribute("tranAmount", 50000),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 100000))),
                       new XElement("account",
                                new XAttribute("accounttype", "moneymarket"),
                                new XAttribute("actbal", 50000),
                                new XAttribute("actbaldate", "5/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "5/1/2012"),
                                        new XAttribute("tranAmount", 50000),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 50000)))),

                     new XElement("user",
                        new XAttribute("name", "Ralph Wolf"),
                        new XAttribute("userID", "rwolf"),
                        new XAttribute("usertype", "Customer"),
                        new XElement("account",
                                new XAttribute("accounttype", "checking"),
                                new XAttribute("actbal", 200),
                                new XAttribute("actbaldate", "4/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "1/1/2013"),
                                        new XAttribute("tranAmount", 100),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 100)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "4/1/2012"),
                                        new XAttribute("tranAmount", 100),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 100))),
                       new XElement("account",
                                new XAttribute("accounttype", "savings"),
                                new XAttribute("actbal", 1000),
                                new XAttribute("actbaldate", "4/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "3/1/2013"),
                                        new XAttribute("tranAmount", 400),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 400)),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "4/1/2012"),
                                        new XAttribute("tranAmount", 600),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 1000))),
                       new XElement("account",
                                new XAttribute("accounttype", "moneymarket"),
                                new XAttribute("actbal", 500),
                                new XAttribute("actbaldate", "5/1/2013"),
                                    new XElement("trans",
                                        new XAttribute("tranDate", "5/1/2012"),
                                        new XAttribute("tranAmount", 500),
                                        new XAttribute("trantype", "deposit"),
                                        new XAttribute("tranBalance", 500)))),
                                        ));

I don't understand why this won't give me a list of trans for a provided userID == efudd and accounttype = checking. 我不明白为什么这不能为我提供提供的userID == efudd和accounttype = check的trans列表。 When I call foreach it startsto iterate over the items, But then it just stops and there is nothing found. 当我调用foreach时,它开始遍历所有项目,但是随后它停止了,没有任何发现。

IEnumerable<Transaction> userAcctInfo = 
    from item in Data.Descendants("user")
    where (string)item.Attribute("usertype") == "Customer" &&
         (string)item.Attribute("userID") == userID
    from accts in item.Descendants("account")
    where (string)accts.Attribute("accounttype") == account
    from trans in item.Descendants("user").Descendants("account").Descendants("trans")
    select new Transaction((DateTime)trans.Attribute("tranDate"),
                            (string)trans.Attribute("trantype"),
                            (decimal)trans.Attribute("tranAmount"),
                            (decimal)trans.Attribute("transBalance"));

What am I doing wrong? 我究竟做错了什么? I have looked at several examples here and i just don't understand. 我在这里看了几个例子,但我听不懂。
thanks 谢谢

  1. You have to add Root property call (or Element("Users") ) at the beginning of the query. 您必须在查询开始时添加Root属性调用(或Element("Users") )。
  2. You should use Elements instead of Descendants when you know where exactly the element is in tree structure. 当您知道元素在树结构中的确切位置时,应使用Elements而不是Descendants
  3. Why did you start querying for trans from the item instead of accts , which is already defined there? 为什么从item而不是从这里已经定义的accts开始查询trans

That one works and returns 3 items 那个工作并且退还3项

var userAcctInfo =
    from item in Data.Root.Elements("user")
    where (string)item.Attribute("usertype") == "Customer" &&
          (string)item.Attribute("userID") == userId
    from accts in item.Elements("account")
    where (string)accts.Attribute("accounttype") == account
    from trans in accts.Elements("trans")
    select new Transaction((DateTime)trans.Attribute("tranDate"),
                           (string)trans.Attribute("trantype"),
                           (decimal)trans.Attribute("tranAmount"),
                           (decimal)trans.Attribute("tranBalance"));

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

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