简体   繁体   中英

accesing the value of properties using reflection

I have an object and I want to write and xml element for each property in the object and the value as a string in the middle:

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();

How can I change the "value" for the propiedad.value x)

尝试:

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

already solved ty men

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM