簡體   English   中英

解析Java地圖 <string, string> 從XML到C#Object

[英]Parse Java Map<string, string> from XML into C# Object

嘿,每個人都有一個關於在C#.net 4 MVC 3中解析一些XML的問題。

我有一個從Java應用程序序列化為XML的Map(HashMap)。

我需要將它解析為dotnet端的一個對象,但似乎無法弄明白。 在我的研究中,我看到你無法序列化為Dictionary<string, string>

其他人建議使用public struct KeyValuePair<K, V>但這似乎不起作用。

還嘗試了[XmlArray("mapConfig")]

其中一個先決條件是我們必須使用System.Xml.Serialization因為我們有一個抽象的messenger類,如果我不是必須的話,我想避免改變它。

如果必須的話,我可能會更改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],]

試着決定這是否是一個更好的結構。

當我解決這個問題時,對此的任何建議都會有所幫助。

謝謝

你可以創建一個序列化的類...所以在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());
        }
    }
}

當然,數組中的第一個字符串將是鍵。

解決方案似乎比我想象的更簡單,但實際上取決於選擇正確的模型屬性組合來正確解析。

這是我決定使用的那個,因為它將每個條目分成它自己的列表項。

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

希望這有助於其他人。 感謝大家的意見和建議。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM