Asp.net 使用C将字典转换为XML#
我的XML文件如下所示:Asp.net 使用C将字典转换为XML#,asp.net,c#-4.0,dictionary,linq-to-xml,idictionary,Asp.net,C# 4.0,Dictionary,Linq To Xml,Idictionary,我的XML文件如下所示: <states> <state name ="Alaska"> <Location Name="loc1"> <Address>testadd1</Address> <DateNTime>d1</DateNTime> </Location> <Location Name="loc2"> <Address>add2<
<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
}));
我已经对字典进行了更改,现在需要将其转换回XML。有人能建议我怎么做吗?你可以做,反之亦然:
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();
这将使您(通过使用数据片段):
测试数据1
d1
地址2
d2
如果您不需要使用IDictionary,我发现使用XmlSerializer
非常容易
型号
[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文档并在字典中循环添加元素”的思路。更复杂的是找到一个为你做这项工作的序列化程序。。。如果你能做相反的事情,我会认为手工操作是很容易的。。。
<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>
[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; }
}
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);
//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();