在c#中使用XML反序列化

时间:2015-09-07 11:07:25

标签: c# xml-deserialization

如何反序列化以下xml: 需要读取AdapterName,ConnectorIndex和ViewType

<?xml version="1.0" encoding="utf-8"?>

<MonitorConfiguration>
<System Type="1"> </System>
<Adapters>
<Adapter Name="P1" ></Adapter>
<Monitors>
<Monitor ConnectorIndex="P1" ViewType="C1"></Monitor>
<Monitor ConnectorIndex="P2" ViewType="C2"></Monitor>
<Monitor ConnectorIndex="P2" ViewType="C2"></Monitor>
<Monitor ConnectorIndex="P2" ViewType="C2"></Monitor>

</Monitors>
<Adapter Name="P2" ></Adapter>
<Monitors>
<Monitor ConnectorIndex="P4" ViewType="C3"></Monitor>
<Monitor ConnectorIndex="P5" ViewType="C5"></Monitor>
<Monitor ConnectorIndex="P6" ViewType="C6"></Monitor>
<Monitor ConnectorIndex="P7" ViewType="C7"></Monitor>
</Monitors>
</Adapters>
</MonitorConfiguration>

使用反序列化方法

2 个答案:

答案 0 :(得分:0)

如果需要使用序列化,首先应该有一个与xml文件具有相同结构的类。 因此,您有一个课程MonitorConfiguration,其中包含AdaptersMoniters等等的列表...

您可能会发现this帖子对于有关xml序列化的基本理解非常有用。

一旦有xml等效类型可用,就可以使用XmlSerializer从xml中创建对象。

    // De-Serializes the request into class object
    public T DeserializeXml<T>(XmlNode xmlToDesearialized)
    {
        if (xmlToDesearialized == null) throw new ArgumentNullException("xmlToDesearialized");
        T deserializedObject = default(T);
        XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));

        using (StringReader stringReader = new StringReader(xmlToDesearialized.OuterXml))
        {
            XmlTextReader xmlTextReader = new XmlTextReader(stringReader);
            deserializedObject = (T)xmlSerializer.Deserialize(xmlTextReader);
        }
        return deserializedObject;
    }

答案 1 :(得分:0)

你可以试试这个 -

<div class="container">
<table>
<tr>
<td>
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
    <img src="http://dummyimage.com/300x200/000/fff" class="img-reponsive" />
          </div>
</td>
<td>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">

              <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>
              <p> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</p>

</div>
</td>
</tr>
</table>
</div>

但从下次开始,在提出问题之前尝试一下。