473,378 Members | 1,140 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Parse Xml to array of objects?

Hei,

Can y provide me with example of how to parse XML to array of objects?

thanks


Nov 17 '05 #1
4 15506
I think you will have to explain more what you want to do.
Are you going to represent XML as self defined objects?
Is it an entire tree(meaning you will need to recreate the structure with a
'binary tree') or is
it a list of nodes(meaning all you need is an array of your defined type
that represent a node).

If the latter, this may give you an idea.
Load the nodes in question into an XmlNodeList instance.
Iterate the list and for each node in the list create an instance of your
Type.
Let's call your Type "NodeRepresentation".
XmlNodeList nodes = doc.DocumentElement.SelectNodes("node");
NodeRepresentation [] reps = new NodeRepresentation [nodes.Count];
for (int i = 0x0; i < reps.Length; i ++)
{
NodeRepresentation rep = new NodeRepresentation();
// Fill the "NodeRepresentation" instance with data from the node here
// or anything else you need to do.
reps [i] = rep;
}
Anyway, if you explain more what you need to do, you will probably get a
more precise answer.
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"kingski" <tz*********@gmail.com> wrote in message
news:uF**************@tk2msftngp13.phx.gbl...
Hei,

Can y provide me with example of how to parse XML to array of objects?

thanks

Nov 17 '05 #2
I have a web service which accept XMLNode as parameter:
<Users>
<User>
<FirstName>Jim</ FirstName >
<LastName>Johnson</ LastName >
<ID>1234</ID>
</User>
<User>
<FirstName>Jane</ FirstName >
<LastName>Bauer</ LastName >
<ID>1234</ID>
</User>
<User>
<FirstName>Tim</ FirstName >
<LastName>Robinson</ LastName >
<ID>1234</ID>
</User>
</Users>

I'd like to create an array of objects from this xml/parameter that comes/:

Person[] p;
p[0].FirstName ->Jim
p[0].LastName ->Johnson
p[0].ID -> 1234
Nov 17 '05 #3
"kingski" <tz*********@gmail.com> wrote in news:uFLWibLRFHA.4028
@tk2msftngp13.phx.gbl:
Can y provide me with example of how to parse XML to array of objects?


XML is not that simple and your question is too generic, but you should look
at System.XML
--
Chad Z. Hower (a.k.a. Kudzu) - http://www.hower.org/Kudzu/
"Programming is an art form that fights back"

Develop ASP.NET applications easier and in less time:
http://www.atozed.com/IntraWeb/
Nov 17 '05 #4
This should give you an idea:
[ WebMethod ]
public void ProcessUsersData ( XmlNode usersNode )
{
XmlNodeList userNodes = usersNode.SelectNodes("User");
User [] users = new User [userNodes.Count];
for (int i = 0x0; i < users.Length; i ++)
{
XmlNode userNode = userNodes [i];
string
id = userNode.SelectSingleNode("ID").InnerText,
firstName = userNode.SelectSingleNode("FirstName").InnerText,
lastName = userNode.SelectSingleNode("LastName").InnerText ;
users [i] = new User(id, firstName, lastName);
}
// Do something with 'users'.
}

// Class 'User'; prototype.
public class User
{
public User ( string id, string firstName, string lastName )
{
ID = id;
FirstName = firstName;
LastName = lastName;
}
public readonly string ID = null;
public readonly string FirstName = null;
public readonly string LastName = null;
}
--
Regards,
Dennis JD Myrén
Oslo Kodebureau
"kingski" <tz*********@gmail.com> wrote in message
news:Oa**************@TK2MSFTNGP12.phx.gbl...
I have a web service which accept XMLNode as parameter:
<Users>
<User>
<FirstName>Jim</ FirstName >
<LastName>Johnson</ LastName >
<ID>1234</ID>
</User>
<User>
<FirstName>Jane</ FirstName >
<LastName>Bauer</ LastName >
<ID>1234</ID>
</User>
<User>
<FirstName>Tim</ FirstName >
<LastName>Robinson</ LastName >
<ID>1234</ID>
</User>
</Users>

I'd like to create an array of objects from this xml/parameter that
comes/:

Person[] p;
p[0].FirstName ->Jim
p[0].LastName ->Johnson
p[0].ID -> 1234

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Kutty Banerjee | last post by:
Hi, MyClass *myclass_=new MyClass; and MyClass::MyClass(int) and no default constructor. My object assignment obviously gives an error. So waht is the correct way to do it without using vectors...
1
by: Rustam Asgarov | last post by:
i have an aray of bites " private byte ByteArray = new byte; " How can i parse this array to read integers, in other words assign first 4 bytes to one integer variable, second 4 bytes to another...
2
by: Raphael Iloh | last post by:
Hi all, I'm having problems comparing array objects. Take a look at this: int array1 = new int{1}; int array2 = new int{1}; Console.Writeln(array1.Equals(array2)); One would expect the above...
10
by: Steve | last post by:
this code: private Array m_arrays = new Array; results in an array of 3 null Array objects. What am I missing?
1
by: plau011 | last post by:
Hi all, I thought I understood .Net Framework with value types and referencetypes but apparently not. So I have this code. Excuse me if it's not 100% correct I cut and paste and shortened it a...
4
Ispep
by: Ispep | last post by:
Hi, unfortunately having a bit of difficulty with a question from an Open University course I'm currently doing. If you could help me out in any way I'd be grafeul (though obviously it goes without...
2
by: shapper | last post by:
Hello, I have the following class ( Level is just a simple enumeration ): public class Theme { public Subject Subject { get; set; } public List<LevelLevels { get; set; } public string Note {...
13
by: milk242 | last post by:
I know I'm making a mistake, but I'm wondering if someone could tell me the type of mistake I'm making. I know I can write the whole array to a file, but I want to create separate binary files for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.