2

I have my XML File as follows:

<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>

I have converted this to the following dictionary as follows:

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

class :

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

}

I have made changes to my dictionary, Now I need to convert this back to XML . Can anyone suggest me how I could go about it?

Krishh
  • 4,111
  • 5
  • 42
  • 52
  • Where exactly is the problem? Simple suggestions on how to go about it would seem to go along the lines of "create an xml document and loop through your dictionary adding elements to it". More complicated ones would be to find a serialiser that does the job for you... I'd have thought just doing it manually is pretty easy if you can do the reverse... – Chris Jan 27 '12 at 17:34
  • [This](http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/cc440cd8-122d-4e90-bfc1-de237c4a4760/) post may get you going. – mreyeros Jan 27 '12 at 17:31

2 Answers2

5

You could do it vise versa:

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

This gets you (by using your data fragment):

<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>
Oleks
  • 31,955
  • 11
  • 77
  • 132
1

If you do not require using an IDictionary, I find it very easy to work with the XmlSerializer.

Models

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

Deserializing objects from 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);

Serializing objects to 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();
Craig
  • 6,869
  • 3
  • 32
  • 52