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
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
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
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).
A way... might help you
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
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
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);
}