Convert Dictionary to XML using C#
-
20-04-2021 - |
Вопрос
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?
Решение
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>
Другие советы
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();
Не связан с StackOverflow