Click here to Skip to main content
12,630,137 members (26,977 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# XML
Hi

i am working with xml in c#
i want to read the xml using XElement
My Xml file is like this
<?xml version="1.0" encoding="UTF-8"?><!--XML GENERATED by IntuitDataSyncEngine (IDS) using \\SBDomainServices\CDM\branches\3.9.0-rel-1-->
<RestResponse xmlns="http://www.intuit.com/sb/cdm/v2"
xmlns:xdb          ="http://xmlns.oracle.com/xdb"
xmlns:xsi          ="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation ="http://www.intuit.com/sb/cdm/v2 ../common/RestDataFilter.xsd">
<Items>
<Item>
<Name>139</Name>
<Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
<Item>
<Name>139</Name>
<Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
<Item>
<Name>139</Name>
<Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
</RestResponse>

from this xml i want the read the items
without "<RestResponse>" node i can easily read the item
but with "<RestResponse>" i can't read the item using XElement
How to read the Items ..? say some Answers
Posted 8-Jan-13 5:19am
Updated 8-Jan-13 18:53pm
v3
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

This is not valid XML.
The <Items> element is opened before each <Item> element, but it isn't closed after each.
Opened 3 times, closed only once.
I'd guess that it should be opened only once, as the container for the <Item> elements.

Edited: I see you updated the example XML.

First: XDocument does not allow the comment to be a top-level element (it should, it is legal), so it will still fail to parse this. Moving the comment to be inside the <RestResponse> element fixes this.

The real issue is due to namespaces. The xmlns attribute in the <RestResponse> tag defines the no-prefix namespace. So that namespace must be used when referring to or filtering by element names.
This shows how (it assumes that the Root element is in the same namespace as the Item elements, i.e. no prefix):
// InputXML is the XML as a string. Use XDocument.Load() for file/stream input.
XDocument source = XDocument.Parse(InputXML);
XName itemName = XName.Get("Item", source.Root.Name.NamespaceName);
foreach (XElement item in source.Descendants(itemName))
{
  Console.WriteLine(item.ToString());
}
This outputs:
<Item xmlns="http://www.intuit.com/sb/cdm/v2">
  <Name>139</Name>
  <Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
<Item xmlns="http://www.intuit.com/sb/cdm/v2">
  <Name>139</Name>
  <Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
<Item xmlns="http://www.intuit.com/sb/cdm/v2">
  <Name>139</Name>
  <Desc>[Sample Product] Elgato EyeTV DTT Deluxe</Desc>
</Item>
Note the explicit namespace attribute in the output.
  Permalink  
v3
Comments
kpkaran88 21-Jan-13 7:17am
   
Hi Matt T Heffron,
Thanks for ur answer. i get the each item from my xml file using xelement but i need one more help.. now i am getting only item in string format. how to get the inner text of Name and Desc
Matt T Heffron 22-Jan-13 13:34pm
   
Once you have each of the Item XElement objects, you can use the item.Element method to get the individual Name and Desc as XElement and use the .Value property to get the text. Like this:
string ns = source.Root.Name.NamespaceName;
XName itemName = XName.Get("Item", ns);
foreach (XElement item in source.Descendants(itemName))
{
Console.WriteLine("The item named: {0} has description: {1}",
item.Element(XName.Get("Name", ns)).Value,
item.Element(XName.Get("Desc", ns)).Value);
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.161205.3 | Last Updated 9 Jan 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100