0

こんにちは、私はこれに 2 日間取り組んできましたが、なぜ機能しないのかわかりません。私は次の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)))),
                                        ));

提供された userID == efudd および accounttype = check のトランスのリストが表示されない理由がわかりません。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"));

私は何を間違っていますか?ここでいくつかの例を見てきましたが、理解できません。
ありがとう

4

1 に答える 1

0
  1. クエリの先頭にRootプロパティ呼び出し (または)を追加する必要があります。Element("Users")
  2. 要素がツリー構造のどこにあるかが正確にわかっている場合は、Elements代わりに使用する必要があります。Descendants
  3. 既に定義されているではなくtransから のクエリを開始したのはなぜですか?itemaccts

それは機能し、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"));
于 2013-08-17T01:29:35.100 に答える