一个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,问题解决。
相关文档:
作者: J. Andrew Schafer
这篇文章假设你对 XML, XSLT, 和 C# 熟悉
下载这篇文章的源代码: XMLC.exe (76KB)
译者说明:这篇文章是很早以前就发表了,它提供的源代码是基于 VS.net 测试版(RTM 和 Beta 2)的。
摘要
C# 允许开发人员在源代码中插入XML注释,这在多人协作开发的时候显得特别有用。 ......
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. ......
当在Perl中使用XML时,你会有将近五百个CPAN模块可以选择,每一个都支持整合Web服务的不同方面。此外,Perl的核心库包括多个支持XML的模块。这篇文章就关注于一个最早期且涉及最频繁的核心模块:XML::Parser.
XML::解析器系列
最初的Perl解析器XML::Parser::Expat由Larry Wall在几年前编写并由Clark Cooper保持延续。模 ......
Web控件是否支持样式表(CSS)呢?
支持,所有的Web控件都从基类System.Web.UI.WebControls.WebControl中继承了一个叫做CssClass的属性。
示例源代码:
<html>
<head>
<style>
.Input { font: 10pt verdana; color: red; }
</style>
</head>
<body>
<form runat=& ......
1.读取XML文件的类:
public class XMLUtils {
private final String DB_XML_FILE = "/XMLSetting.xml";
public Properties getPropertiesfromXML() {
URL url = XMLUtils.class.getResource(dBXMLFILE);
URI uri;
try {
uri = url.toURI();
InputSource xmlfile = new InputSource(uri.g ......