User987
User987

Reputation: 3823

XDocument C# parse node value inside node

I have an XML document which basically looks like this:

<Item>
 <Seller>
   <UserID>SomeSeller</UserID>
   <FeedbackScore>2535</FeedbackScore>
 </Seller>
</Item>

Now I'm trying to parse the document like following:

 var document = XDocument.Parse(upcList);
                    XNamespace ns = "urn:ebay:apis:eBLBaseComponents";
                    var result= document
                        .Descendants(ns + "Item")
                        .Select(item => new CustomClass
                        {
                            FeedBackScore = Convert.ToInt32(item.Descendants(ns+ "Seller")
                                            .Where(p=>p.Name.LocalName=="FeedbackScore").FirstOrDefault()),             

                            Sales = (int) item.Element(ns+"QuantitySold"),
                            Location = (string)item.Element(ns+"Location"),
                            CurrentPrice = (double)item.Element(ns + "CurrentPrice"),
                            Title = (string)item.Element(ns + "Title"),
                            ItemID = (string)item.Element(ns + "ItemID")

                        }).ToList();

Please note this part how I try to parse the FeedbackScore node value:

   FeedBackScore = Convert.ToInt32(item.Descendants(ns+ "Seller")
                                            .Where(p=>p.Name.LocalName=="FeedbackScore").FirstOrDefault()),   

But when I try to parse it I'm getting all "FeedbackScore" nodes values as "0" :(

Can someone tell me what am I doing wrong and how can I fetch this value inside this node "2535"?

Upvotes: 0

Views: 167

Answers (3)

Charles Mager
Charles Mager

Reputation: 26223

Descendants here will return Seller elements, and then you check if any of them have the name FeedbackScore. This isn't possible - they can't have two names at once.

Assuming you want the FeedbackScore only if the parent is Seller, you need to read the Elements of the Seller elements.

I'd also note you can use the explicit conversions in the same way you do for other properties.

Putting that together, this would work:

FeedBackScore = (int) item.Elements(ns + "Seller")
    .Elements(ns + "FeedbackScore")
    .Single()

If this element isn't always present, you can default to 0:

FeedBackScore = (int?) item.Elements(ns + "Seller")
    .Elements(ns + "FeedbackScore")
    .SingleOrDefault() ?? 0;

Upvotes: 1

LittleBit
LittleBit

Reputation: 1201

You have mistakenly checked the names of the Seller Nodes not its Children. By doing so, the FirstOrDefault() will yield null(condition of Where() is never met due the wrong Node) and Convert.ToIn32(null) will yield 0.

To fix this, you can go for the "FeedbackScore" Node directly and Convert its Value like this

FeedBackScore = Convert.ToInt32(item.Descendants("FeedBackValue").FirstOrDefault()?.Value),

Upvotes: 1

Django
Django

Reputation: 222

FeedBackScore = Convert.ToInt32(item.Descendants(ns + "FeedbackScore").Value)

Upvotes: 1

Related Questions