2012-01-27 30 views
1

を使用してXMLに辞書を変換します次のように私は私のXMLファイルを持っているC#

<states> 
<state name ="Alaska"> 
    <Location Name="loc1"> 
    <Address>testadd1</Address> 
    <DateNTime>d1</DateNTime> 
    </Location> 
    <Location Name="loc2"> 
    <Address>add2</Address> 
    <DateNTime>d2</DateNTime> 
    </Location> 
</state> 
</states> 

次のように私は、次の辞書にこれを変換しています

 XDocument doc = XDocument.Load(Server.MapPath("test2.xml")); 

     IDictionary<string, Dictionary<string, Property>> dictionary = doc.Root.Elements("state").ToDictionary(
      s => s.Attribute("name").Value, 
      s => s.Elements("Location").ToDictionary(
       loc => loc.Attribute("Name").Value, 
       loc => new Property 
       { 
        address = loc.Element("Address").Value, 
        datetime = loc.Element("DateNTime").Value 
       })); 

クラス:

public class Property 
{ 
    public string address; 
    public string datetime; 

} 

私は辞書に変更を加えましたが、今これをXMLに変換する必要があります。誰も私はそれについて行くことができる方法を私に提案することはできますか?

+0

からオブジェクトをデシリアライズ

[XmlRoot(ElementName="states")] public class Container { [XmlElement("state")] public List<State> States { get; set; } } public class State { [XmlAttribute()] public string name { get; set; } [XmlElement("Location")] public List<Location> Locations { get; set; } } public class Location { [XmlAttribute()] public string Name { get; set; } public string Address { get; set; } public DateTime DateNTime { get; set; } } 

問題?それについての簡単な提案は、「XML文書を作成し、辞書に要素を追加する」という行に沿っているように見えます。もっと複雑なものは、あなたのために仕事をするシリアライザを見つけることです...私はあなたが逆のことをすることができれば、手動で行うことはかなり簡単だと思っていたでしょう... – Chris

+0

[This](http:// social。 msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cc440cd8-122d-4e90-bfc1-de237c4a4760/)ポストがあなたを得るかもしれません。 – mreyeros

答えて

4

あなたはそれがその逆行うことができます:

var result = new XDocument(new XElement("states", 
    dictionary.Select(i => new XElement("state", new XAttribute("name", i.Key), 
     i.Value.Select(v => new XElement("Location", new XAttribute("Name", v.Key), 
      new XElement("Address", v.Value.address), 
      new XElement("DateNTime", v.Value.datetime) 
    )) 
)) 
)); 

var xml = result.ToString(); 

これは、(あなたのデータの断片を使用して)あなたを取得します。あなたがたIDictionaryを使用して必要としない場合は

<states> 
    <state name="Alaska"> 
    <Location Name="loc1"> 
     <Address>testadd1</Address> 
     <DateNTime>d1</DateNTime> 
    </Location> 
    <Location Name="loc2"> 
     <Address>add2</Address> 
     <DateNTime>d2</DateNTime> 
    </Location> 
    </state> 
</states> 
1

、私はそれが非常に簡単に見つけますXmlSerializerで作業します。

モデル正確であるXML

var xml = @"<?xml version='1.0' encoding='utf-16'?> 
        <states> 
        <state name ='Alaska'> 
         <Location Name='loc1'> 
         <Address>testadd1</Address> 
         <DateNTime>2012-01-01</DateNTime> 
         </Location> 
         <Location Name='loc2'> 
         <Address>add2</Address> 
         <DateNTime>2012-01-01</DateNTime> 
         </Location> 
        </state> 
        </states>"; 

var stream = new System.IO.StringReader(xml); 

var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Container)); 

var container = serializer.Deserialize(stream); 

XMLへのオブジェクトのシリアル化

//create and populate the container with your data here 
//probably created when you hydrate your object from XML as in above. 
var container = new Container(); 

//used to clean up unneeded namespaces 
var xmlSerializerNamespace = new System.Xml.Serialization.XmlSerializerNamespaces(); 
xmlSerializerNamespace.Add(string.Empty, string.Empty); 

var serializer = new System.Xml.Serialization.XmlSerializer(typeof(Container)); 

var stream = new System.IO.StringWriter(); 

//serialize your object to a stream 
serializer.Serialize(stream, container, xmlSerializerNamespace); 

var yourXml = stream.ToString(); 
+0

あなたのお返事ありがとうございました – Krishh

+0

問題ない歓声! – Craig

関連する問題