By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,435 Members | 2,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,435 IT Pros & Developers. It's quick & easy.

Read Xml file in c# and store attribute values in arrays

P: 1
I had xml file. How to read the xml file in c# and after reading the file we can store the xml attribute names and values are stored in arrays. I am newer to programming language.. So please help me to write the code in c# by using windowsApllications only..

My Xml file is given below..
Expand|Select|Wrap|Line Numbers
  1. <GlobalActivities>
  2.   <Activity Name="LoadImage" LibraryName="Activity.dll" ClassName="Activity.LoadImage" IsDrop="false">
  3.     <Shape Name="LoadImageShape" LibName="DiagramControl.dll" ClassName="LoadImageShape">
  4.     </Shape>
  5.   </Activity>
  6. <Activity Name="SaveImage" LibraryName="Save.Activity.dll" ClassName="Activity.SaveImage" IsDrop="true">
  7.     <Shape Name="SaveImageShape" LibraryName="DiagramControl.dll" ClassName="SaveImageShape">
  8.     </Shape>
  9.   </Activity>
  10. <Activity Name="EditImage" LibraryName="Edit.Activity.dll" ClassName="Activity.EditImage" IsDrop="false">
  11. </Activity>
  12. </GlobalActivity>
In the above code some activities didn't contain shape. In these case shape element is false, so that it is no need to store all shape attribute valus otherwise it store all shape attribute values..
Aug 3 '11 #1
Share this Question
Share on Google+
2 Replies

Paul Johnson
P: 97
What code have you currently got around this?
Aug 4 '11 #2

P: 4
You're making an assumption that you must store the values somewhere.
If you use the XmlDocument class, all nodes and attributes are accessible as appropriately named collections in the XmlDocument you loaded the file into. No need to convert it into any sort of static arrays.
Expand|Select|Wrap|Line Numbers
  1. using System.Xml;
Then to actually load the file something like this:
Expand|Select|Wrap|Line Numbers
  1. XmlDocument doc = new XmlDocument();
  2. try
  3. {
  4.  doc.Load(FilePath);
  5. }
  6. catch (Exception err)
  7. {
  8.  MessageBox.Show(err.Message);
  9. }
Where FilePath is the path to your XML. Then check the attributes of doc.
Aug 12 '11 #3

Post your reply

Sign in to post your reply or Sign up for a free account.