将Java Map <string,string =“”>从XML解析为C#Object </string,>

时间:2013-03-06 17:21:23

标签: c# java .net xml xml-serialization

嘿,每个人都有一个关于在C#.net 4 MVC 3中解析一些XML的问题。

我有一个从Java应用程序序列化为XML的Map(HashMap)。

我需要将它解析为dotnet端的一个对象,但似乎无法弄明白。在我的研究中,我发现你无法序列化为Dictionary<string, string>

其他人建议public struct KeyValuePair<K, V>,但似乎没有用。

还尝试了[XmlArray("mapConfig")]

其中一个先决条件是我们必须使用System.Xml.Serialization,因为我们有一个抽象的信使类,如果我不是必须的话,我想避免改变它。

如果必须的话,我可能会更改Java对象,如果这样可以使这更容易,但如果可能的话,宁可使用已存在的那个。如果它有助于Java层使用Xstream。

以下是从Java发送的一大块XML

<ConfigurationMap>
    <mapConfig class="hashtable">
      <entry>
        <string>Key1</string>
        <string>Value1</string>
      </entry>
      <entry>
        <string>Key2</string>
        <string>Value2</string>
      </entry>
      <entry>
        <string>Key3</string>
        <string>Value3</string>
      </entry>
      <entry>
        <string>Key4</string>
        <string>Value4</string>
      </entry>
    </mapConfig>
</ConfigurationMap>

谢谢,如果您需要更多信息,请告诉我。期待答案。

- UPDATE -

我认为这很明显,但我应该提到XML是以字符串形式提到的抽象消息中回来的。目前的方法使用:

XmlDocument doc = new XmlDocument();
doc.LoadXml(this.ResponseXml);
XmlElement main = doc.DocumentElement;

XmlElement cse = util.getElementsFirstChild(main, "MessagePayload");
XmlElement ccs = util.getElementsFirstChild(cse, "ReturnedObjectNameHERE");

然后,我们使用模型上的System.Xml属性对片段进行反序列化。

以下是我们使用的模型的一个简单示例:

[XmlRoot("list")]
public class SearchResults : List<CSearchResult>
{
    public SearchResults() { }
}

[XmlRoot("SearchResult")]
public class SearchResult
{
    [XmlElement("Id")]
    public string OrgUnitId { get; set; }

    [XmlElement("Type")]
    public Type Type { get; set; }

    [XmlElement("Name")]
    public string Name { get; set; }

    [XmlElement("Description")]
    public string Description { get; set; }
}

- 更新2 - 我能够使用下面的这个模型类获得一些不错的模型绑定

[XmlRoot("ConfigurationMap")]
public class ConfigurationMap
{
    [XmlElement("mapConfig")]
    public MapConfig mapConfig { get; set; }

}

[XmlRoot("mapConfig")]
public class MapConfig
{
    [XmlArray("entry")]
    public List<string> entry { get; set; }
}

唯一的问题是Object属性mapconfig只是将所有条目聚集在一个列表中,这对于模型是有意义的。

试图搞乱数组类型,看看我是否能获得更好的结果。期待MapConfig成为Entry的数组。我可以保留Entry列表或使Entry成为一个数组,以便对象结构变为:

MapConfig: [[key1, value1], [key2, value2], [key3, value3], [key4, value4],]

尝试确定这是否是一种更好的结构。

对此我提出的任何建议都会有所帮助。

由于

2 个答案:

答案 0 :(得分:2)

您可以创建一个序列化为...的类 所以在xml上运行xsd.exe ...

C:\>xsd test.xml
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.17929]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\test.xsd'.

C:\>xsd test.xsd /classes
Microsoft (R) Xml Schemas/DataTypes support utility
[Microsoft (R) .NET Framework, Version 4.0.30319.17929]
Copyright (C) Microsoft Corporation. All rights reserved.
Writing file 'C:\test.cs'.

然后只使用简单的c#XmlSerializer ......

NewClassName object = xml.DeSerializeStringToObject<NewClassName>();

下面的助手类

public static class XmlSerializerHelper
{
    public static T DeSerializeStringToObject<T>(this string sxml)
    {
        using (XmlTextReader xreader = new XmlTextReader(new StringReader(sxml.Replace("&", "&amp;"))))
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            return (T)xs.Deserialize(xreader);
        }
    }

    public static string SerializeObjectToString(this object obj)
    {
        using (MemoryStream stream = new MemoryStream())
        {
            XmlSerializer x = new XmlSerializer(obj.GetType());
            x.Serialize(stream, obj);
            return Encoding.Default.GetString(stream.ToArray());
        }
    }
}

当然,数组中的第一个字符串将是键。

答案 1 :(得分:0)

解决方案似乎比我想象的更简单,但实际上取决于选择正确的模型属性组合来正确解析。

这是我决定使用的那个,因为它将每个条目分成它自己的列表项。

[XmlRoot("ConfigurationMap")]
public class ConfigurationMap
{
    [XmlArray("mapConfig")]
    [XmlArrayItem("entry")]
    public List<Entry> MapConfig { get; set; }

}

[XmlRoot("entry")]
public class Entry
{
    [XmlElement("string")]
    public List<string> entry { get; set; }
}

希望这可以帮助别人。感谢大家的投入。

相关问题