Need to load XML into Arrays

pGustafson
pGustafson used Ask the Experts™
on
I have loaded an XML file into an array. I am confused on how to split the array into multiple fields.

I want to choose which items to display from the array.
c# Code
	private void ReadFile2Array()
	{
		// Buzz
		int arrLength = 5; 
		String[] news_array = new String[arrLength];
		XmlDocument xmlDoc = new XmlDocument();
		xmlDoc.Load(Server.MapPath("~/news.xml"));


		XmlNodeList nodes = xmlDoc.SelectNodes("News/Item");
		int i = 0; 
		foreach (XmlNode node in nodes)
		{
			i=i++;
			XmlNode story = node.SelectSingleNode("story");
			XmlNode url = node.SelectSingleNode("url");
			news_array[i]=story.InnerText + " "	+ url.InnerText;
			
	}
		Response.Write(news_array[i]);
}

XML file:
<?xml version="1.0" encoding="UTF-8"?>
<News>
	<Item>
		<story>Story A</story>
		<url>URL A</url>
	</Item>
		<Item>
		<story>Story B</story>
		<url>URL B</url>
	</Item>
		<Item>
		<story>Story c</story>
		<url>URL C</url>
	</Item>
		<Item>
		<story>Story D</story>
		<url>URL d</url>
	</Item>
		<Item>
		<story>Story E</story>
		<url>URL e</url>
	</Item>
</News>

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Reza RadConsultant, Trainer

Commented:
I have no idea about what you want excatly
could you provide example of expected array?

Author

Commented:
I basically need to separate story and URL so I can call them separately.
Reza RadConsultant, Trainer

Commented:
so, you need two dimension array
on dimension for story, and another one for url.

try this for example:

int arrLength = 5;
            String[,] news_array = new String[arrLength,2];
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"D:\news.xml");


            XmlNodeList nodes = xmlDoc.SelectNodes("News/Item");
            int i = 0;
            foreach (XmlNode node in nodes)
            {
                i = i++;
                XmlNode story = node.SelectSingleNode("story");
                XmlNode url = node.SelectSingleNode("url");
                news_array[i,0] = story.InnerText;
                news_array[i,1]=url.InnerText;

            }
Response.Write(news_array[i,0]);
Response.Write(news_array[i,1]);

Open in new window

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Author

Commented:
so I would like it to look like:

news_array[0,0] = "Story";
news_array[0,1] = "URL";

Does that make sense, or am I going about this the wrong way?
Reza RadConsultant, Trainer

Commented:
you must change line 4 of my code with your equivalent code, i changed it for testing on my machine only

Author

Commented:
That looks to be working great, the only thing that I might have coded wrong is if I do:

Response.Write(news_array[0,0]);
Response.Write(news_array[0,1]);

I only get the last item in the XML file.
Reza RadConsultant, Trainer

Commented:
so you must write values of arrays in a loop like this:

// Buzz
            int arrLength = 5;
            String[,] news_array = new String[arrLength,2];
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"D:\news.xml");


            XmlNodeList nodes = xmlDoc.SelectNodes("News/Item");
            int i = 0;
            foreach (XmlNode node in nodes)
            {
                i = i++;
                XmlNode story = node.SelectSingleNode("story");
                XmlNode url = node.SelectSingleNode("url");
                news_array[i,0] = story.InnerText;
                news_array[i,1]=url.InnerText;

            }

            for (int c1 = 0; c1 < arrLength; c1++)
            {
                Response.Write(news_array[i, 0].ToString());
                Response.Write(news_array[i, 1].ToString());
            }

Open in new window

Consultant, Trainer
Commented:
sorry, the code had some problems,
now fixed,
try this one, i changed your code more than before

int arrLength = 5;
            String[,] news_array = new String[arrLength,2];
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(@"D:\news.xml");


            XmlNodeList nodes = xmlDoc.SelectNodes("News/Item");
            int i = 0;
            foreach (XmlNode node in nodes)
            {
                
                XmlNode story = node.SelectSingleNode("story");
                XmlNode url = node.SelectSingleNode("url");
                news_array[i,0] = story.InnerText;
                news_array[i,1]=url.InnerText;
                i++;
            }

            for (int c1 = 0; c1 < arrLength; c1++)
            {
                Response.Write(news_array[c1, 0].ToString());
                Response.Write(news_array[c1, 1].ToString());
            }

Open in new window

Author

Commented:
For some reason I am still only getting the last item in the XML.

If I leave it as:
                  Response.Write(news_array[i, 0].ToString());
                  Response.Write(news_array[i, 1].ToString());
I get:
Barnes & Noble Says It Does Not Share Data With Discount ClubsURL EBarnes & Noble Says It Does Not Share Data With Discount ClubsURL EBarnes & Noble Says It Does Not Share Data With Discount ClubsURL EBarnes & Noble Says It Does Not Share Data With Discount ClubsURL EBarnes & Noble Says It Does Not Share Data With Discount ClubsURL E

If I change it to:
                  news_array[0, 0].ToString()
I get: Barnes & Noble Says It Does Not Share Data With Discount Clubs
which is the last item story  in my XML
Reza RadConsultant, Trainer

Commented:
try my last code, that one is correct

Author

Commented:
Sorry, I must have mixed up some of the code. It works great. :-)

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial