2

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

我已将其转换为以下字典,如下所示:

        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 。谁能建议我怎么做?

4

2 回答 2

5

反之亦然:

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

这让你(通过使用你的数据片段):

<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>
于 2012-01-27T20:55:08.390 回答
1

如果您不需要使用 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 反序列化对象

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();
于 2012-01-27T22:58:18.377 回答