易截截图软件、单文件、免安装、纯绿色、仅160KB

一个C# xml 序列化错误

一个C# xml 序列化错误
 
事发现场:
xml序列化的数据中存储的节点数据是
   <Module>536870912</Module>  (xml文件中)
对应的类属性是
    public short Module { get; set; }  (C#类中)
 
序列化的代码:
public static FMDSTimeSeriesDefinitionList Deserialize(string xml)
{
XmlSerializer serializer = new XmlSerializer(typeof(FMDSTimeSeriesDefinitionList));
/* If the XML document has been altered with unknown
nodes or attributes, handle them with the
UnknownNode and UnknownAttribute events.*/
serializer.UnknownNode += new
XmlNodeEventHandler(serializer_UnknownNode);
serializer.UnknownAttribute += new
XmlAttributeEventHandler(serializer_UnknownAttribute);
FMDSTimeSeriesDefinitionList tsList = null;
using (StringReader sr = new StringReader(xml))
{
try
{
tsList = (FMDSTimeSeriesDefinitionList)serializer.Deserialize(sr);
}
catch (Exception ex)
{
throw ex;
}
}
return tsList;
}
 
 
调试时会弹出异常信息:
An unhandled exception of type 'System.InvalidOperationException' occurred in FMTimeSeriesDefinition.exe
Additional information: There is an error in XML document (293, 8).
找到xml文件的293行第8列:
292:      <Module>536870912</Module>
293:      <DurationPeriodsFlag>1</DurationPeriodsFlag>
294:      <DecimalFlag>0</DecimalFlag>
 
是一个"<"标记符,看不出问题,再看前一个节点,突然发现536870912 存入short中,数据溢出。
将short改为int,问题解决。


相关文档:

C# XML注释

作者: J. Andrew Schafer
这篇文章假设你对 XML, XSLT, 和 C# 熟悉
下载这篇文章的源代码: XMLC.exe (76KB)
译者说明:这篇文章是很早以前就发表了,它提供的源代码是基于 VS.net 测试版(RTM 和 Beta 2)的。
摘要
    C# 允许开发人员在源代码中插入XML注释,这在多人协作开发的时候显得特别有用。 ......

怎样将XML的Document 转换成 String

import java.io.StringWriter;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.apache.xerces.dom.DocumentImpl;
import org.apache.xerces.dom.DOMImplementationImpl;
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.Serializer;
import org.apache.xml. ......

如何在 XML 文件中取得指定属性的相关值?

两种方法:
其一,使用 SelectNodes 的方法;以下例子为使用此方法的处理。
其二,使用 XQuery 的方法。
    ''' <summary>
    ''' 从 XML 文件中取得对应ID 的语言值
    ''' </summary>
    ''' <param name="textID">输入的ID< ......

C#.NET的Web控件是否支持CSS

Web控件是否支持样式表(CSS)呢?
支持,所有的Web控件都从基类System.Web.UI.WebControls.WebControl中继承了一个叫做CssClass的属性。
示例源代码:
<html>
<head>
<style>
.Input { font: 10pt verdana; color: red; }
</style>
</head>
<body>
<form runat=& ......

C#读写xml文件

已知有一个XML文件(bookstore.xml)如下:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book genre="fantasy" ISBN="2-3631-4">
    <title>Oberon's Legacy</title>
    <author>Corets, Eva</author>
  &nb ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号