user1765862
user1765862

Reputation: 14185

Extract value from xml file, simple example

structure of my xml file

<?xml version="1.0" encoding="utf-8"?>
<ItemsToProcess xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Item>somevalue1</Item>
  <Item>somevalue2</Item>
</ItemsToProcess>

I tried to extract values like this

 XElement elem = XElement.Load(filename);
 var items = from c in elem.Descendants("ItemsToProcess")                     
             select new ItemToProcess
             {
                 ItemValue = c.Element("Item").Value;
             };
             return items;

but obviously I'm missing something. What I'm doing wrong?

Upvotes: 0

Views: 60

Answers (1)

Selman Gen&#231;
Selman Gen&#231;

Reputation: 101711

You have a xml namespace that you need to specify.And if you need Item elements you can just use :

XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";
var items = elem.Descendants(ns + "Item")
           .Select(x => new ItemToProces { ItemValue = x.Value });                    

See this to find more information about xml namespaces.

Upvotes: 2

Related Questions