通用XML到对象映射

时间:2013-06-20 01:06:48

标签: c# xml converter

我收到一条xml消息,将其翻译成其他格式,然后发送。消息的范围从大约40行到600行或更多行xml,平均大约100行。我可以一秒钟收到多条消息,但在繁重的时间平均每分钟大约15到20分钟。

由于xml为现有应用程序提供了新信息,因此我创建了一个模仿输出xml结构的类。该对象将创建输出xml,仅包括已更改的项,并将输入术语转换为消费应用程序将理解的语言。我弄清楚的是如何轻松地将传入的xml映射到对象。

传入的xml使用几种不同的模板来确定每个节点的格式。我正在尝试创建一个地图,可以确定如果节点名为n,那么它需要转到对象m。下面是我想要做的简化示例。

消息1

<Incoming>
    <Name>Bob</Name>
    <City>Seattle</City>
    <Hobby>Fishing</Hobby>
</Incoming>

消息2

<Incoming>
    <Name>Bob</Name>
    <Employment>
        <JobTitle>Sales</JobTitle>
        <Manager>Jessica</Manager>
    </Employment>
    <Hobby>Reading</Hobby>
</Incoming>

这将进入类似于此的对象:

public Customer
{
    public String Name{get; set;}
    public Address CustomerAddress{get;set;}
    public Employer CustomerEmployer{get;set;}
    public List<String> Hobbies{get;set;}
}

public Address
{
    public String StreetAddress{get;set;}
    public String City{get;set;}
    public String State{get;set;}
    public String Zip{get;set;}
}

public Employer
{
    public String Company{get;set;}
    public String Position{get;set;}
    public String Manager{get;set;}
    public Address CompanyAddress{get;set;}
}

如果没有创建一个很长的Switch Case,有没有人有关于如何最好地从xml获取信息到对象的想法?由于信息量的增加,我对处理的时间成本有了更多的了解。

我想过想出一个映射;

之类的东西
<Name>Customer:Name</Name>
<City>Customer:Address:City</City>

然而,存在如何映射列表中的项目的问题,例如Hobby。还有如何快速使用映射的问题。我唯一能想到的是每个对象处理地图的一部分以确定路径,虽然这听起来很昂贵。

我并不担心不同级别的重复地址。这个数据就是一个例子。在我的实际xml中,我认为我没有任何重复。

我感谢人们提供的任何帮助或想法。提前谢谢。

1 个答案:

答案 0 :(得分:2)

我能够使用反射和递归来使用地图访问属性。我设置了这样的地图路径:

map = new Dictionary<string, string>();
map.Add("Name", "Name");
map.Add("Street", "Address.Address");
map.Add("City", "Address.City");
map.Add("State", "Address.State");
map.Add("Zip", "Address.Zip");
map.Add("Activity", "*Hobbies.Hobby");
map.Add("Years", "*Hobbies.Years");

星号表示这是一个列表,需要一个密钥。我在处理中添加了密钥,因此我发送的完整路径类似于“* Hiking.Hobbies.Years”,其中徒步旅行是关键。处理此问题的方法如下:

private void SetValue(object source, String path, String value)
{
    if (path.Contains('.'))
    {
        //  If this is not the ending Property, continue recursing
        int index = path.IndexOf('.');
        String property = path.Substring(0, index);

        object nextSource;
        if(property.Contains("*"))
        {
            path = path.Substring(index + 1);
            index = path.IndexOf('.');
            String dictionaryName = path.Substring(0, index);
            property = property.Substring(1);

            IDictionary list = source.GetType().GetProperty(dictionaryName).GetValue(source, null) as IDictionary;
            if (!list.Contains(property))
            {
                Type[] arguments = list.GetType().GetGenericArguments();
                list.Add(property, Activator.CreateInstance(arguments[1]));
            }

            nextSource = list[property];                    
        }
        else
            nextSource = source.GetType().GetProperty(property).GetValue(source, null);

        SetValue(nextSource, path.Substring(index + 1), value);
    }
    else
    {
        PropertyInfo pi = source.GetType().GetProperty(path);
        pi.SetValue(source, Convert.ChangeType(value, pi.PropertyType), null);
    }
}

我希望这可以帮助别人。

相关问题