views:

46

answers:

6

Hello All,

i need to convert an XML into an array using asp.net.

I tried searching on the web but couldn't find the proper solution.

Can someone please help me with this

Thanks,
Alloi

A: 

Alloi - i think that due to the complexity inherent in xml structures, this will very much depend on the xml itself. what i'm saying is that a generic solution may be very difficult to make bomb proof, whereas a solution to a 'known' structure may be fairly straightfwd. the serialiser may allow you to save it to ToList() or similar but beyond that, it's a case of horses for sources i think. :)

jim

[edit] - link that may be useful: http://www.c-sharpcorner.com/UploadFile/chauhan_sonu57/SerializingObjects07202006065806AM/SerializingObjects.aspx

also - a nice little link showing how to convert xml->json in c# : http://www.phdcc.com/xml2json.htm

jim
A: 

If you have an XSD to which the XML refers to, that probably can be a starting point on the implementation of array (or list).

Shiva
A: 

A way... might help you

  1. Read xml into dataset.
  2. Iterate dataset on the basis of table rows and add each row entity to a list.
  3. display list as array or list itself.
Amit Ranjan
A: 

I don't know if there is a .net class that performs this action directly or not but in case there is no you have 2 options :

1 . Load XML into dataset then loop in the dataset and set elements of the array.

2 . Get XML nodes number then loop through XML and read each node element then put it in the array.

Hope that this is useful

Ahmy
+1  A: 

I think LinqToXML might be helpful in your situation. Here's a sample in vb.net (I could not find anything in csharp...): http://msdn.microsoft.com/en-us/vbasic/bb738047.aspx#convnodesarray

silvo
Here's a C# example - http://www.dev102.com/2008/04/25/linq-to-xml-in-3-easy-steps/
IanR
+1  A: 

I find LinqToXML helpful in these circumstances. I have a very simple XML document:

<?xml version="1.0" encoding="utf-8" ?>
<People>
  <Person>
    <id>1</id>
    <name>Dave</name>
  </Person>
  <Person>
    <id>2</id>
    <name>Pete</name>
  </Person>
  <Person>
    <id>3</id>
    <name>Marie</name>
  </Person>
</People>

And the following code that will turn it into an array of objects:

FileStream file = new FileStream("data.xml", FileMode.Open);

XDocument xmldoc = XDocument.Load(file);

var people = (from p in xmldoc.Descendants("Person") 
              select new 
              { 
              ID = p.Element("id").Value, 
              Name = p.Element("name").Value 
              }).ToArray();

foreach (var person in people)
{
    Console.WriteLine(person.Name);
}
DavidGouge
david - nice one. works well with the structure in the scenario. much as i said in my little ditty below, if you know the structure, you're set. it's the unknown structure that causes the issue i feel. but nice answer...
jim
Thanks a lot! :)
Alloi
Thanks @jim, and no probs @Alloi.
DavidGouge