开发者

accesing the value of properties using reflection

开发者 https://www.devze.com 2023-02-07 04:54 出处:网络
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:

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)


Try:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消