0

I am trying to pull an event description in XML, but I am having trouble accessing the data.

I am trying to access the eventDetailsValue element.

Here is a sample of my code:

(version 1)

 XElement doc = XElement.Parse(e.Result);

evtDesc = doc.Element("eventDetails").Element("eventDetails").Element("eventDetailsValue").Element("eventDetailsValue").Value;

(version2)

XElement doc = XElement.Parse(e.Result);

var xGood = from detaildoc in doc.Descendants("eventDetails")
from d in detaildoc.Elements("eventDetail").Elements("eventDetailsValue")
select d;

I have tried the following for a different element and it worked:

GeoLat = Convert.ToDouble(doc.Element("latitude").Value);

Here is a sample of the xml result (i removed the values for simplicity):

<event>
  <longitude></longitude> 
  <latitude></latitude> 
  <category></category> 
  <dma></dma> 
  <activeAdvantage></activeAdvantage> 
  <seoUrl></seoUrl> 
  <assetID></assetID> 
  <eventID></eventID> 
  <eventDetailsPageUrl></eventDetailsPageUrl> 
- <mediaTypes>
  <mediaType></mediaType> 
  <mediaType></mediaType> 
  <mediaType></mediaType> 
  <mediaType></mediaType> 
  <mediaType></mediaType> 
  </mediaTypes>
  <eventContactEmail /> 
  <eventContactPhone /> 
  <eventName></eventName> 
  <eventDate></eventDate> 
  <eventLocation></eventLocation> 
  <eventAddress></eventAddress> 
  <eventCity></eventCity> 
  <eventState></eventState> 
  <eventZip></eventZip> 
  <eventCountry></eventCountry> 
  <usatSanctioned></usatSanctioned> 
  <regOnline></regOnline> 
  <eventCloseDate></eventCloseDate> 
  <currencyCode></currencyCode> 
  <eventTypeID></eventTypeID> 
  <eventType></eventType> 
  <hasEventResults></hasEventResults> 
  <hasMetaResults></hasMetaResults> 
  <showMap></showMap> 
  <eventContactEmail /> 
  <eventContactPhone /> 
  <displayCloseDate></displayCloseDate> 
  <excludedFromEmailing></excludedFromEmailing> 
  <regOpensMessage /> 
  <regFunnel></regFunnel> 
  <isValid></isValid> 
  <displayRegistration></displayRegistration> 
- <channels>
- <channel>
  <channelName></channelName> 
  <primaryChannel></primaryChannel> 
  </channel>
  </channels>
- <eventDetails>
- <eventDetail>
  <eventDetailsName></eventDetailsName> 
  <eventDetailsOrder></eventDetailsOrder> 
  <eventDetailsValue></eventDetailsValue> 
  </eventDetail>
- <eventDetail>
  <eventDetailsName></eventDetailsName> 
  <eventDetailsOrder></eventDetailsOrder> 
  <eventDetailsValue></eventDetailsValue> 
  </eventDetail>
  </eventDetails>
  <eventDonationLinks /> 
  <eventSanctions /> 
- <eventCategories>
- <eventCategory>
  <categoryID></categoryID> 
  <categoryGroupCount></categoryGroupCount> 
  <categoryName></categoryName> 
  <categoryType></categoryType> 
  <categoryOrder></categoryOrder> 
  <numRegistered></numRegistered> 
  <maxRegistrations></maxRegistrations> 
  <percentFull></percentFull> 
  <displayDate></displayDate> 
  <closeDate></closeDate> 
  <actualCloseDate></actualCloseDate> 
  <isExpired></isExpired> 
- <priceChanges>
- <priceChange>
  <price></price> 
  <priceUntilDate></priceUntilDate> 
  </priceChange>
  </priceChanges>
  </eventCategory>
  </eventCategories>
  <eventUrl></eventUrl> 
  <eventContactUrl></eventContactUrl> 
  <eventImageUrl></eventImageUrl> 
  </event>

Any help would be appreciated!

4

2 回答 2

0

Try this query:

var xGood = from detaildoc in doc.Descendants("eventDetails")
select new 
{
Value = detaildoc.Elements("eventDetail").Elements("eventDetailsValue").Value
};
于 2012-04-12T17:02:03.957 回答
0

The eventDetailsValue is in an array of eventDetail elements. So you need to diferentiate which element in the array you want. With this (and these LinqToXml extensions: http://searisen.com/xmllib/extensions.wiki) you can write it like this:

XElement doc = XElement.Parse(e.Result);
var details = doc.GetEnumerable("eventDetails/eventDetail", x => new
{
    Name = x.Get("eventDetailsName", string.Empty),
    Order = x.Get("eventDetailsOrder", string.Empty),
    Value = x.Get("eventDetailsValue", string.Empty)
});

details is an IEnumerable<object> of Name's, Order's and the Value(s) you want. You can now loop through details and get the value(s) you want. I made Name, Order and Value all be strings, but by calling Get<type>("name", defaultValueByType) you can have them be other types instead.

You can loop through them like this:

foreach(var detail in details)
{
    string value = detail.Value;
}

GetEnumerable is shorthand (in this case) for:

doc.Element("eventDetails").Elements("eventDetail").Select(x => new ...)

But it does null checking for you, which if your xml always produces the above xml, there would be no problems to do it long hand. And Get returns the proper value.

Note: Because this is a WindowsPhone7 project, you'll have to set a compiler flag of WindowsPhone7 so that the extensions compile without complaint (hopefully/I haven't tested it).

于 2012-04-12T19:35:16.747 回答