1

我收到一条 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 中,我认为我没有任何重复项。

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

4

1 回答 1

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

我希望这可以帮助某人。

于 2013-07-16T21:20:19.533 回答