Click here to Skip to main content
Rate this: bad
good
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 4:19am
Edited 8-Jan-13 17:53pm
v3
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 at 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 at 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);
}
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

  Permalink  

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

  Print Answers RSS
0 Maciej Los 295
1 OriginalGriff 278
2 Nguyen.H.H.Dang 230
3 Peter Leow 195
4 arvind mepani 187


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