Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read Xml data

Status
Not open for further replies.

AgentM

MIS
Jun 6, 2001
387
US
am trying to read the following xml file into different arrays

Here's the xml file format

<Elements>
<elem1 attr1="val1" attr2="val2">
<subelem1 attr="val1">
<type1 />
<value1 />
</subelem1>
<subelem2 attr="val2">
<type2 />
<value2 />
</subelem2>
</elem1>

<elem2 attr4="val4" attr5="val5" />
<subelem1 attr="val1">
<type1 />
<value1 />
</subelem1>

<subelem2 attr="val2">
<type2>
<value2>
</subelem2>
</elem2>
</Elements>

I need to read each Element in a different array and the subelements are goingto be objects in each cell in the array.

How can I accomplish this using only xmlTextReader.?

Any ideas are welcome.

 
This will return a SortedList, I'm sure it can be converted to return an Array or ArrayList without to much effort.
Code:
public class SortedListBuilder{
		
/// <summary>
/// This method reads the xml file looking for the requested key group and returns them setup as a SortedList.
/// </summary>
/// <param name="xmlFile">The file where we are getting our keys.</param>
/// <param name="keyGroup">The name of the keys we are looking for.</param>
/// <returns>A SortedList with key-value</returns>
public static SortedList MakeSortedList(string xmlFile, string keyGroup){
			
SortedList sortedList = new SortedList();
		
// read values from xml config file and load keys into sorted list
if(System.IO.File.Exists(xmlFile)){

	string val = "";
	string keyName = "";

	XmlTextReader MyReader = new XmlTextReader(xmlFile);
			MyReader.WhitespaceHandling=WhitespaceHandling.None;
	MyReader.Read();
				
	while(MyReader.Read()){
					MyReader.MoveToElement();
	val = MyReader.Value;
	if(MyReader.NodeType==XmlNodeType.Element){
						
		if(MyReader.Name==keyGroup){
		keyName = MyReader["name"].ToString();
		MyReader.Read();
		val = MyReader.Value;
		sortedList.Add(keyName,val);
		MyReader.Read();
		}
	}
}
MyReader.Close();
}
return sortedList;
}
}

Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top