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

C# writing attribute value into the element of the xml file

P: 30
Dear All,

I am trying to write the xml file described as below

XML for UnSelectedGameList.XML
Expand|Select|Wrap|Line Numbers
  1. <UnSelectedGameList>
  3.   <game id="1" name="A" picture=@E:\Games\1.png" isSelected="0">
  4.   </game>
  6. </UnSelectedGameList>
I would like to update the attribute value of id and isSelected using data from left and right of list box controls. Then write as a new selectedgamelist.xml.

XML for SelectedGameList.XML
Expand|Select|Wrap|Line Numbers
  1. <SelectedGameList>
  3.   <game id="1" name="A" picture=@"\Games\1.png" isSelected="0">
  4.   </game>
  6. </SelectedGameList>
I am looking forward to get any advice from someone in here.

Thanks and best regards
Jan 2 '09 #1
Share this Question
Share on Google+
4 Replies

Expert 100+
P: 229
To make your properties serialize as attributes, you need to use the [XmlAttribute] attribute when defining your class, in your example it would be something like:

Expand|Select|Wrap|Line Numbers
  1. /*
  2. using System.Xml;
  3. using System.Xml.Serialization;
  4. */
  6. public class Game
  7. {
  8.     private string _id;
  9.     [XmlAttribute]
  10.     public string ID
  11.     {
  12.         get { return _id; }
  13.         set { _id = value; }
  14.     }
  16.     private string _name;
  17.     [XmlAttribute]
  18.     public string Name
  19.     {
  20.         get { return _name; }
  21.         set { _name = value; }
  22.     }
  24.     // you get the idea
  25. }
If you really need to have different xml element names for "selected" and "unselected" items, you can create something like this:

Expand|Select|Wrap|Line Numbers
  1. [XmlType("UnselectedGameList")]
  2. public class UnselectedGameList : List<Game> { }
  4. [XmlType("SelectedGameList")]
  5. public class SelectedGameList : List<Game> { }
I presume you already know how to serialize/deserialize objects to/from xml. If not, here is a simple generic class that I use to do it quickly:


You can add it to your project and then use it to serialize/deserialize your data:
Expand|Select|Wrap|Line Numbers
  1. // read from a file
  2. UnselectedGameList unselected =
  3.     XmlHelper<UnselectedGameList>.Deserialize(@"UnselectedGameList.xml");
  5. // write to a file
  6. SelectedGameList selected = new SelectedGameList();
  7. XmlHelper<SelectedGameList>.Serialize(selected, @"SelectedGameList.xml");
Note that these two files will have different Xml schemas, so you will always need two separate classes to handle them (what I am trying to say - I would normally use only a single file with a single Xml schema).
Jan 2 '09 #2

Expert 100+
P: 190
vekipeki - that is an awesome response...handle all the attributes as properties of a class, then serialize, deserialize the object to xml - whoa...too cool.

That said, my take on the post was the more old fashioned: I have an xml file, I want to load it from disk, find the correct element, update some attributes, and write it back to disk.

For this you can use several classes from the System.Xml library as needed:
XmlDocument to load and save a physical file from and to disk;
XmlReader/Writer to read and write, if necessary.
XmlNode and XmlAttributeCollection to find a specific attribute and modify it.

There are several similar approaches. Just to get you started, have a look at the example that comes with XmlNode.Attributes Property

For small documents, these classes are fine. For large documents and heavy xml processing, the System.Xml.XPath library is recommended, especially the XPathNavigator.

If you can handle it, however, vekipeki's solution is the way to go.
Jan 2 '09 #3

P: 30
Hi vekipeki and mldisibio

Thank you for ur reply but I have still errors as below after
instance = (T)xml.Deserialize(sr);

There is an error in XML document (1, 2).

As for me, I am quite new for xmlSerializer and mldisibio is ok only for char like
<game id="1" name="A" picture=" 1.png" isSelected="0"> </game>

my application is to update the string of id = "1", "2" and so on
and isSelected="0" to "1" or "1" to "0"

I am looking for your advice coz I am in a hurry mode now.

Best regards
Jan 3 '09 #4

Expert 100+
P: 229
Hello again, I am sorry I didn't read your message earlier, I just returned from my vacation.

If you are still working on this, I believe that the problem could be the exact character casing (e.g. "Game" should be named "game", "ID" should be "id", according to your original file). I am sorry I didn't pay enough attention to the exact element names - my mistake.

"There is an error in XML document (1, 2)." error tells you that the unexpected character was found in row 1, column 2 (that would be "g" if your file starts with "<game", which would indicate wrong casing, but open your .xml file in Notepad just to check exactly where the problem is). Check the exact spelling for "UnselectedGameList" also.

You can either rename your class and properties, or add the desired Xml element names in the attributes for each property:

Expand|Select|Wrap|Line Numbers
  1. [XmlType("game")] // this will serialize Game as "game"
  2. public class Game
  3. {
  4.     private string _id;
  5.     [XmlAttribute("id")] // this will serialize ID as "id"
  6.     public string ID
  7.     {
  8.         get { return _id; }
  9.         set { _id = value; }
  10.     }
  12.     // ...
  13. }
Also, if you want to omit xml namespace declaration (and Xml version <?xml version="1.0"?>) , check Scott Hanselman's Computer Zen - XmlFragmentWriter - Omiting the Xml Declaration and the XSD and XSI namespaces
for an example of how to extend XmlTextWriter to skip "xmlns" and similar elements.
Jan 12 '09 #5

Post your reply

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