[Mono-list] Modify XML Docs

Miguel de Icaza miguel@ximian.com
05 Oct 2003 14:38:24 -0400


Hello,

> Im creating an app (mBloggy, to post 'posts' to a blog), however I'm
> going to use a xml file to save the user configuration (the blogs).
> Today I can write (JUST new files), read it and identify some parts, but
> I dont undestand what the inet says about modifying xml files, so How
> can I modify an xml file? to modify nodes and remove elements, for
> example:
> 
> <mbloggy>
>  <account>
>   <username>Pablo</username>
>   <password>Crypted</password>
>   <identifier>Personal WebSite</identifier>
>   <url>http://pablo.com.mx/xmlrpc.php</url>
>  </account>
>  <account>	
> 	... the same..
>   </account>
> </mbloggy>

This sounds like a prime candidate for XmlSerialization, use:

public class mbloggy {
	public Account [] accounts;
}

public class account {
	public string username;
	public string password;
	public string identifier;
	public string url;
}

mbloggy m = new mbloggy ();
account a = new Account;
m.accounts = new Account [1];
m.accounts [0] = a;
a.username = "Miguel";
a.identifier = "...";

XmlSerializer s = new XmlSerializer (typeof (mbloggy));
s.Serialize (Console.Out, m);

Miguel