2017-05-08 3 views
0

Ich habe die folgende XML:spezifische Elemente aus einem XML-Dokument

<?xml version="1.0" encoding="utf-8" ?> 
<catalog> 
    <books> 
<book id="bk101"> 
    <author id="1">Gambardella, Matthew</author> 
    <title>XML Developer's Guide</title> 
    <genre>Computer</genre> 
    <cover-price> 
    <price> 
     <country>US</country> 
     <amount>44.95</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>39.99</amount> 
    </price> 
    </cover-price> 
    <unit-price> 
    <price> 
     <country>US</country> 
     <amount>25.00</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>20.00</amount> 
    </price> 
    </unit-price> 
    <publish_date>2000-10-01</publish_date> 
    <description> An in-depth look at creating applications with XML.</description> 
</book> 
<book id="bk102"> 
    <author id="2">Ralls, Kim</author> 
    <title>Midnight Rain</title> 
    <genre>Fantasy</genre> 
    <cover-price> 
    <price> 
     <country>US</country> 
     <amount>5.95</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>3.99</amount> 
    </price> 
    </cover-price> 
    <unit-price> 
    <price> 
     <country>US</country> 
     <amount>2.50</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>2.00</amount> 
    </price> 
    </unit-price> 
    <publish_date>2000-12-16</publish_date> 
    <description> A former architect battles corporate zombies,an evil sorceress, and her own childhood to become 
queen of the world.</description> 
</book> 
<book id="bk103"> 
    <author id="3">Corets, Eva</author> 
    <title>Maeve Ascendant</title> 
    <genre>Fantasy</genre> 
    <cover-price> 
    <price> 
     <country>US</country> 
     <amount>5.95</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>3.99</amount> 
    </price> 
    </cover-price> 
    <unit-price> 
    <price> 
     <country>US</country> 
     <amount>2.50</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>2.00</amount> 
    </price> 
    </unit-price> 
    <publish_date>2000-11-17</publish_date> 
    <description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for 
a new society. </description> 
    </book> 

<book id="bk104"> 
    <author id="4">Corets, Eva</author> 
    <title>Oberon's Legacy</title> 
    <genre>Fantasy</genre> 
    <cover-price> 
    <price> 
     <country>US</country> 
     <amount>5.95</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>3.99</amount> 
    </price> 
    </cover-price> 
    <unit-price> 
    <price> 
     <country>US</country> 
     <amount>2.50</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>2.00</amount> 
    </price> 
    </unit-price> 
    <publish_date>2001-03-10</publish_date> 
    <description>In post-apocalypse England, the mysterious agent known only as Oberon helps to create a new life for 
the inhabitants of London. Sequel to Maeve Ascendant.</description> 

    </book> 

<book id="bk105"> 
    <author id="5">Corets, Eva</author> 
    <title>The Sundered Grail</title> 
    <genre>Fantasy</genre> 
    <cover-price> 
    <price> 
     <country>US</country> 
     <amount>5.95</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>3.99</amount> 
    </price> 
    </cover-price> 
    <unit-price> 
    <price> 
     <country>US</country> 
     <amount>2.50</amount> 
    </price> 
    <price> 
     <country>UK</country> 
     <amount>2.00</amount> 
    </price> 
    </unit-price> 
    <publish_date>2001-09-10</publish_date> 
    <description>The two daughters of Maeve, half-sisters, battle one another for control of England. Sequel to 
Oberon's Legacy.</description> 

    </book> 
    </books> 
</catalog> 

ich folgendes verwenden, um ein bestimmtes Buch wählen

var selectedBook = from r in document.Descendants("book").Where 
            (r=>(string)r.Attribute("id")=="bk102") 
         select new 
         { 
          Author = r.Element("author").Value, 
          Title = r.Element("title").Value, 
          Genere = r.Element("genre").Value, 
          Price = r.Element("price").Value, 
          PublishDate = r.Element("publish_date").Value, 
          Description = r.Element("description").Value, 

         }; 

Wie kann ich wählen Sie anschließend die Abdeckung Preis Menge Buch für die USA? Jede Kombination habe ich versucht, kehrt nur alle vier der Menge Felder

+0

Sie müssen manuell entfernen, die Preise, die, wie alle Kinder werden nicht Ihren Wünschen entsprechen inbegriffen. – CodeCaster

+0

Bitte reduzieren Sie Ihre XML auf nur ein oder zwei Bücher - es hat keinen Vorteil, all diese Daten zu haben. –

Antwort

1

Sie sind in der Nähe

var selectedBook = from r in document.Descendants("book").Where 
            (r=>(string)r.Attribute("id")=="bk102") 
         select new 
         { 
          Author = r.Element("author").Value, 
          Title = r.Element("title").Value, 
          Genere = r.Element("genre").Value, 
          Price = r.Element("price").Value, 
          PublishDate = r.Element("publish_date").Value, 
          Description = r.Element("description").Value, 
          USPrice = r.Element("cover-price") 
             .Descendants("country") 
             .Where(c => (string)c == "US")) 
             .Select(c => (decimal)c.Parent.Element("amount")) 
             .First() 
         }; 
+0

Ich denke, Sie müssen die "Menge" Kind Element auswählen ... –

+0

@JonSkeet Ja Herr, ich bin müde. Vielen Dank. –

1
  • Derzeit versucht man, ein price Element direkt unter book zu holen, das ist also nicht mit zu beginnen zur Arbeit zu gehen.
  • Als nächstes wollen Sie nur wählen Sie das price Element mit der rechten country Unterelement
  • Schließlich wollen Sie nicht den Wert des price Element, möchten Sie den Wert seines amount Kind-Element - und ich würde immer lassen vermuten, dass eine als decimal und nicht als String

so dies das heißt statt:

Price = r.Element("price").Value 

Sie wollen so etwas wie:

Price = (decimal) r.Element("cover-price") 
        .Elements("price") 
        .Single(p => (string) p.Element("country") == "US") 
        .Element("amount") 

Das noch scheitern wird, wenn es nicht irgendwelche price Elemente mit dem richtigen Land. Sie können dies stattdessen eine geeignete decimal? zu erhalten:

Price = (decimal?) r.Element("cover-price") 
        .Elements("price") 
        .SingleOrDefault(p => (string) p.Element("country") == "US") 
        ?.Element("amount") 
Verwandte Themen