0

i am trying loading and saving data by Robert Harvey code in this topic. i can save. but load process is not success full.

i have tried :

var list = XmlHelper.FromXmlFile<List<Item>>(@"c:\folder\file.xml");

i did not find the correct namespase for Item

var list = XmlHelper.FromXmlFile<List<Array>>(@"c:\folder\file.xml");
{"Object reference not set to an instance of an object."}
var list = XmlHelper.FromXmlFile<List<ArrayList>>(@"c:\folder\file.xml");
{"<ArrayOfAnyType xmlns=''> was not expected."}
var list = XmlHelper.FromXmlFile<List<Double>>(@"c:\folder\file.xml");
{"<ArrayOfAnyType xmlns=''> was not expected."}

but all of them have error which listed below them.

i want to retrieve those number in form of arraylist or double[];

the XML content : enter image description here

Community
  • 1
  • 1
majid mobini
  • 107
  • 1
  • 8
  • I don't know about XmlHelper. Look into XDocument instead. XDocument looks simpler in usage to me. Not that I am an expert or anything. – SKull Oct 09 '13 at 08:24

2 Answers2

1

First Load the Document:

var doc = XDocument.Load("c:\somefile.xml");

Then you can access the Elements with

XElement xe = doc.Element("Name of the Element");

If you got more than one Element with the same name you can get them with:

IEnumerable<XElement> xe = doc.Elements("Name of the Element");

You can Access Attributes kind of similar:

XAttribute xa = doc.Attribute("Name of the Attribute");

and

IEnumerable<XAttribute> xa = doc.Attributes("name");

don't forget to always do null checks.

I hope this helps.

SKull
  • 331
  • 2
  • 12
0

for loading the data from xml file use the below process if you are using c#

XElement xelement = XElement.Load("..\\..\\XML1.xml");
Neel
  • 11,625
  • 3
  • 43
  • 61
  • 1
    I agree. That's why i suggested XDocument instead in the comment above. With XDocument you can access every XElement and XAttribute that is in the xml file. – SKull Oct 09 '13 at 08:28
  • it loads all the xml as a single variable with one node. So how can i access to the numbers. – majid mobini Oct 09 '13 at 08:41
  • it has a value property that its value is type string and added all numbers to gether like this( it is a single string) : "216.23737589973135209.67775014847356106.6266524689597299.760910704962214302.68822465901223293.86403464104819" – majid mobini Oct 09 '13 at 08:45
  • @majidmobini just go thorugh this document i hope it will help http://www.dotnetcurry.com/ShowArticle.aspx?ID=564 – Neel Oct 09 '13 at 08:46
  • @Neel the problem is here that the xml load as a single element so the code in the document dose not work for it. – majid mobini Oct 09 '13 at 08:58
  • @Majid Mobini: Yeah. That's why its better to use XDocument.Load() instead of XElement. XDocument should contain ALL Elements and Attributes. I am no expert but I'll try to post a short answer. – SKull Oct 10 '13 at 08:54