c# - Adding a String Indexer to XmlSerializer -


i have xml source can't change , want deserialise using xmlserializer.

i can fine there arrays of custom classes access array string , not in integer.

i know can use

public classname this[string index]  

but can't work out add class.

i want able call

object.transaction["transactiontypename"]  

instead of

object.transaction[0] 

this stripped down version of class.

public partial class configuration {     private configurationtransaction[] transactionsfield;      [system.xml.serialization.xmlarrayitemattribute("transaction", isnullable = false)]     public list<configurationtransaction> transactions     {                 {             return this.transactionsfield;         }         set         {             this.transactionsfield = value;         }     } }  public partial class configurationtransaction {     private string typefield;      [system.xml.serialization.xmlattributeattribute()]     public string type     {                 {             return this.typefield;         }         set         {             this.typefield = value;         }     }  } 

a simplified example code of classes indexer. while need save value access indexer have create additional class.

public class configuration {     public configurationtransaction this[string transactionname]     {                 {             return transactions.first(tran => tran.transactionname == transactionname).configurationtransaction;         }         set         {             int index = transactions.findindex(tran => tran.transactionname == transactionname);             if (index >= 0)                 transactions[index] = new pairhelper { transactionname = transactionname, configurationtransaction = value };             else                 transactions.add(new pairhelper { transactionname = transactionname, configurationtransaction = value });         }     }     [editorbrowsable(editorbrowsablestate.never)]     public list<pairhelper> transactions { get; set; } }  public class configurationtransaction {     [xmlattribute()]     public string type { get; set; } }  public class pairhelper {     public string transactionname { get; set; }     public configurationtransaction configurationtransaction { get; set; } } 

it's work:

configuration conf = new configuration(); conf.transactions = new list<pairhelper>(); conf["footran"] = new configurationtransaction { type = "foo" }; conf["bartran"] = new configurationtransaction { type = "bar" };  var xs = new xmlserializer(typeof(configuration)); using (var fs = new filestream("test.txt", filemode.create)) {     xs.serialize(fs, conf); }  configuration conf2; using (var fs = new filestream("test.txt", filemode.open)) {     conf2 = (configuration)xs.deserialize(fs); }  foreach (var tran in conf2.transactions)     console.writeline(tran.transactionname + " : " + tran.configurationtransaction); console.writeline(conf2["footran"].type); console.writeline(conf2["bartran"].type); 

xml this:

<?xml version="1.0"?> <configuration xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:xsd="http://www.w3.org/2001/xmlschema">   <transactions>     <pairhelper>       <transactionname>footran</transactionname>       <configurationtransaction type="foo" />     </pairhelper>     <pairhelper>       <transactionname>bartran</transactionname>       <configurationtransaction type="bar" />     </pairhelper>   </transactions> </configuration> 

Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -