2011-01-25 64 views
0

我有一個對象,我想在對象中的每個屬性和值作爲在中間的字符串寫入和XML元素的屬性的值:accesing使用反射

System.Type type = cabecera.GetType(); 
     System.Reflection.PropertyInfo[] propiedades = type.GetProperties(); 

     xml.WriteStartDocument(); 
     xml.WriteStartElement("Factura"); 
      xml.WriteStartElement("CABFAC"); //inicio de cabecera 

      // imprime inicio valor y fin de elemento por cada propiedad del objeto 
      foreach (System.Reflection.PropertyInfo propiedad in propiedades) 
      { 
       xml.WriteStartElement(propiedad.Name); 
       xml.WriteString("value"); // here is the problem 
       xml.WriteEndElement(); 
      } 


      xml.WriteEndElement(); //fin de factura 
     xml.WriteEndDocument(); 
     xml.Close(); 

我怎樣才能改變「價值」 爲propiedad.value X)

回答

2

嘗試:

xml.WriteString(propiedad.GetValue(cabecera, null).ToString()); 
+0

ty x)已經解決了 – 2011-01-26 00:01:08

0

已經解決了TY男人

xml.WriteStartElement(propiedad.Name); object o = propiedad.GetValue(cabecera,null); if(o!= null) xml.WriteString(o.ToString()。Trim()); xml.WriteEndElement();