反序列化xml文档正在更改xml中节点的值

时间:2013-04-25 12:31:43

标签: deserialization

我有一个xml,我根据xml中的文本更新了treeview。 示例xml:

<TaskSeverity></TaskSeverity>
<TaskCategory></TaskCategory>
<TaskTitle></TaskTitle>
<TaskMessage></TaskMessage>
<TaskCode></TaskCode>
<TaskName>SQL_MAPPING_COMPANIES</TaskName>
<Schema>CLIENT</Schema>
<!-- Schema is a required field -->
<Mapping>Companies</Mapping>
<!-- Mapping is a required field -->
<CacheDb>false</CacheDb>
<!-- CacheDb is an optional field. Default value is false -->
<DeleteTableBeforeExecute>true</DeleteTableBeforeExecute>
<!-- DeleteTableBeforeExecute is an optional field. Default value is false -->

现在我有一种方法可以做到这一点。

public void UpdateTreeView(XmlDocument xDoc)
      {
         xmlTreeViewAdv.Nodes.Clear();
         _nodeToTaskDictionary.Clear();
         if (xDoc == null)
            return;
         TreeNodeAdv rootNode = new TreeNodeAdv(xDoc.DocumentElement.Name);
         if (rootNode.Text.Equals("DmtTask") && xDoc.DocumentElement != null)
         {
            foreach (XmlAttribute attribute in xDoc.DocumentElement.Attributes)
            {
               if (attribute.Name.Equals("xsi:type"))
               {
                  rootNode.Text = attribute.Value;
               }
            }
         }



   xmlTreeViewAdv.Nodes.Add(rootNode);


      rootNode.Font = new Font(rootNode.Font, FontStyle.Bold);

_nodeToTaskDictionary.Add(rootNode, DmtTaskToolbox.FromXml(xDoc));
//This particular line does the deserialization part.

}

// _nodeToTaskDictionary是

 public Dictionary<TreeNodeAdv, DmtTask> _nodeToTaskDictionary = new Dictionary<TreeNodeAdv, DmtTask>();

//where TreeNodeAdv is the Treeview (Syncfusion) and DmtTask is an abstract class

public static class DmtTaskToolbox
   {
      public static DmtTask FromXml(XmlDocument xDoc)
      {
         DmtTask t = DmtTask.DmtXmlSerializer.Deserialize(new XmlNodeReader(xDoc)) as DmtTask;
          // As soon as the above line is executed the value of the last node DeleteTableBeforeExecute innerText is changing to False. I could not understand the reason for it
         if (t == null)
            throw new Exception("Unable to convert the specific XML document DmtTask");
         return t;
      }
   }

public static XmlSerializer DmtXmlSerializer = new XmlSerializer(typeof(DmtTask),DmtTaskTypes);

根据该节点的值,我将从表中删除整个记录并插入新记录。但它的回归是假的。

这是反序列化后的内容

<TaskSeverity></TaskSeverity>
<TaskCategory></TaskCategory>
<TaskTitle></TaskTitle>
<TaskMessage></TaskMessage>
<TaskCode></TaskCode>
<TaskName>SQL_MAPPING_COMPANIES</TaskName>
<Schema>CLIENT</Schema>
<!-- Schema is a required field -->
<Mapping>Companies</Mapping>
<!-- Mapping is a required field -->
<CacheDb>false</CacheDb>
<!-- CacheDb is an optional field. Default value is false -->
<DeleteTableBeforeExecute>false</DeleteTableBeforeExecute>
<!-- DeleteTableBeforeExecute is an optional field. Default value is false -->

非常感谢任何帮助

1 个答案:

答案 0 :(得分:0)

问题在于抽象类中定义的公共变量DeleteTableBeforeExectue与我试图反序列化的xmldocument中定义的标签DeleteTableBeforeExecute的拼写不同。

将类序列化为XML时,每个公共属性和字段值都将转换为XML元素。元素的名称与属性的名称匹配。 XmlElement属性允许修改XML标记的名称和格式