Convert XML to Object Using Reflection

If you like problems to solve, here's a big one: D

First, it's not about serialization, okay?

Well, my situation ... I am writing a function that I will pass as an Xml parameter (XmlDocument) and an object (Object) as a reference. It will return to me an object (the referenced object) populated with values ​​from Xml (XmlDocument).

For instance:

I have Xml:

<user>
  <id>1</id>
  <name>Daniel</name>
</user>

I also have my function

public Object transformXmlToObject (XmlDocument xml, Object ref)
{
  // Scroll each parameters in Xml and fill the object(ref) using reflection.
  return ref;
}

How will I use it?

I will use it like this:

[WebMethod]
public XmlDocument RecebeLoteRPS(XmlDocument xml)
{
  // class user receive the object converted from the function
  User user = new User();
  user = transformXmlToObject(xml, user);

  // object filled 
}

I need some help, please.

Regards, Dan

+3
source share
5 answers

Um, yes, it's about serialization. In fact, this is exactly what XML serialization was written for.

, , , XML? .. Id Name, , XML-?

+1

, .

using System.Xml;
using System.Reflection;
using System.ComponentModel.DataAnnotations;
using System.Collections;

namespace MyProject.Helpers
{
    public class XMLHelper
    {
        public static void HydrateXMLtoObject(object myObject, XmlNode node)
        {
            if (node == null)
            {
                return;
            }

            PropertyInfo propertyInfo;
            IEnumerator list = node.GetEnumerator();
            XmlNode tempNode;

            while (list.MoveNext())
            {
                tempNode = (XmlNode)list.Current;

                propertyInfo = myObject.GetType().GetProperty(tempNode.Name);

                if (propertyInfo != null) {
                    setProperty(myObject,propertyInfo, tempNode.InnerText);

                }
            }
        }
    }
}
+6

User - , XML, , XML.

, User , XML-, ExpandoObject .NET 4.0. XML ExpandoObject.

+1

, , , . , XML- , LINQ to XML.

0

, @andyuk. , , xml, .

, xml , . xsd xml , , .

                    var pi = entity.GetType( ).GetProperty( eleProperty.Name.LocalName );
                    if ( pi.PropertyType == typeof( DateTime ) )
                    {
                        DateTime val = DateTime.MinValue;
                        DateTime.TryParse( ele.Value, out val );
                        pi.SetValue( entity, val, null );
                    }
                    else if ( pi.PropertyType == typeof( Int32 ) )
                    {
                        int val = 0;
                        Int32.TryParse( eleProperty.Value, out val );
                        pi.SetValue( entity, val, null );
                    }
                    else if ( pi.PropertyType == typeof( bool ) )
                    {
                        bool val = false;
                        Boolean.TryParse( eleProperty.Value, out val );
                        pi.SetValue( entity, val, null );
                    }
                    else
                    {
                        pi.SetValue( entity, eleProperty.Value, null );
                    }
0

Source: https://habr.com/ru/post/1755059/


All Articles