当我序列化一个包含DateTimein 的对象时,它在XML字符串中返回空.
请参阅下面的XSD,从XSD生成的可序列化类,以及处理XSD序列化的序列化助手类.
XSD:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="test" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="testInformation">
<xs:complexType>
<xs:sequence>
<xs:element name="DateOfBirth" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:date">
<xs:pattern value="\d{4}-\d{2}-\d{2}" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
串行:
/// <summary>
/// This static class provides methods which can be used to help with
common xml serialiazation tasks.
/// </summary>
public static class XmlSerializationHelper
{
public static string
SerializeObject<ObjectToSerialize>(ObjectToSerialize
obj)
{
string responseXML = string.Empty;
using (MemoryStream ms = new MemoryStream())
using (StreamWriter output = …Run Code Online (Sandbox Code Playgroud) 以下代码指定从基类"TestBase"派生的类型"MyBase64Binary"
using System;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
namespace Test
{
public class TestBase
{
public TestBase()
{
}
}
[XmlType(TypeName = "base64Binary"), Serializable]
public partial class MyBase64Binary : TestBase
{
[System.Xml.Serialization.XmlTextAttribute(DataType = "base64Binary")]
[EditorBrowsable(EditorBrowsableState.Advanced)]
public Byte[] __Value;
[XmlIgnore]
public Byte[] Value
{
get { return __Value; }
set { __Value = value; }
}
public MyBase64Binary()
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试创建这样的XmlSerializer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
namespace Test1
{
class Program …Run Code Online (Sandbox Code Playgroud) 我有一个网站,只要它到达我的代码中的以下位置就会抛出OutOfMemoryExceptions:
XmlSerializer xs = new XmlSerializer(t, xoverrides);
Run Code Online (Sandbox Code Playgroud)
看到这只发生在Web服务器上时,我没有大量关于为什么会发生这种情况的信息.我知道它正在序列化的对象并不是太严肃 - 绝对不到每个MB.
你之前有这个吗?觉得帮我诊断一下这个问题?任何帮助表示赞赏.
谢谢!
我有以下DOM
<row>
<link href="Büro.txt" target="_blank">
my link
</link>
</row>
Run Code Online (Sandbox Code Playgroud)
当我使用Java XmlSerializer将它序列化为一个文件时,它出现如下:
<row>
<link href="B&#252;ro.txt" target="_blank">
my link
</link>
</row>
Run Code Online (Sandbox Code Playgroud)
有没有办法控制XmlSerializer处理属性转义的方式?我应该以任何方式做到这一点吗?
更新
我还应该说我使用的是jre 1.6.直到最近我一直在使用jre 1.5,我很确定它是'正确'序列化(即'&'未被转义)
澄清
DOM是以编程方式创建的.这是一个例子:
Document doc = createDocument();
Element root = doc.createElement("root");
doc.appendChild(root);
root.setAttribute("test1", "ê");
root.setAttribute("test2", "üöä");
root.appendChild(doc.createTextNode("ê"));
StringWriter sw = new StringWriter();
serializeDocument(doc, sw);
System.out.println(sw.toString());
Run Code Online (Sandbox Code Playgroud)
我的解决方案
我并不是真的想这样做,因为它涉及大量的代码更改和测试,但我决定将属性数据移动到CDATA元素中.问题解决避免了.
问题:我在http://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx上使用可序列化的字典类 来序列化字典.哪个工作正常,但我遇到了一个恼人的问题.
<System.Xml.Serialization.XmlRoot("DataBase")> _
Public Class cDataBase
<System.Xml.Serialization.XmlNamespaceDeclarations()> _
Public ns As New System.Xml.Serialization.XmlSerializerNamespaces()
<System.Xml.Serialization.XmlElement("Tables")> _
Public Tables1 As New SerializableDictionary(Of String, cTable)
End Class ' cDataBase
Run Code Online (Sandbox Code Playgroud)
当我序列化上述类的实例时,创建的xml如下所示:
<Tables>
<item>
<key>
<string>MyTable</string>
</key>
<value>
<Table CreationDate="0001-01-01T00:00:00" LastModified="0001-01-01T00:00:00">
<Columns>
<Column Name="PrimaryKeyName">
<DataType>Uniqueidentifier</DataType>
<Length>36</Length>
</Column>
</Columns>
<Rows>
<Row>
<Item>Reihe1</Item>
<Item>Reihe2</Item>
<Item>Reihe3</Item>
</Row>
<Row>
<Item>Reihe1</Item>
<Item>Reihe2</Item>
<Item>Reihe3</Item>
</Row>
Run Code Online (Sandbox Code Playgroud)
如果我能弄清楚如何将键从<string>重命名为属性中定义的内容,那将会很好
<key>
<string>MyTable</string>
</key>
Run Code Online (Sandbox Code Playgroud)
基本上类似于XmlArrayItem属性,如下面,if(仅)字典是一个数组...
<System.Xml.Serialization.XmlArray("Tables")> _
<System.Xml.Serialization.XmlArrayItem("Table")> _
Public Tables As New List(Of cTable)
Run Code Online (Sandbox Code Playgroud)
我想尝试将字符串更改为从字符串继承的自定义类,我可以配备一个名称,但问题是,不能从字符串继承...
我有一个包含我想用XmlSerializer序列化的数组的类:
[XmlArray("properties")]
[XmlArrayItem("property", IsNullable = true)]
public List<Property> Properties { get; set; }
Run Code Online (Sandbox Code Playgroud)
Property是一个包含属性的类,有些XmlText:
[XmlAttribute("name")]
public string Name { get; set; }
[XmlText]
public string Value { get; set; }
Run Code Online (Sandbox Code Playgroud)
问题是,当Value为null时,它序列化为空字符串:
<property name="foo" />
Run Code Online (Sandbox Code Playgroud)
而不是null.我正在寻找要完全省略的值,或者看起来像这样:
<property name="foo" xsi:nil="true" />
Run Code Online (Sandbox Code Playgroud)
是否可以根据其XmlText值清空列表中的元素?我真的想避免自定义序列化,但在这种情况下,或许其他一些序列化框架会更好?
调用
List<PC> _PCList = new List<PC>();
...add Pc to PCList..
WriteXML<List<PC>>(_PCList, "ss.xml");
Run Code Online (Sandbox Code Playgroud)
功能
public static void WriteXML<T>(T o, string filename)
{
string filePath= Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Genweb2\\ADSnopper\\" + filename;
XmlDocument xmlDoc = new XmlDocument();
XPathNavigator nav = xmlDoc.CreateNavigator();
using (XmlWriter writer = nav.AppendChild())
{
XmlSerializer ser = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("TheRootElementName"));
ser.Serialize(writer, o); // error
}
File.WriteAllText(filePath,xmlDoc.InnerXml);
}
Run Code Online (Sandbox Code Playgroud)
内在的例外
无法转换类型为'System.Collections.Generic.List
1[PC]' to type 'System.Collections.Generic.List1 [System.Collections.Generic.List`1 [PC]]'的对象.
请帮忙
我正在将MusicXML文件加载到我的程序中.问题:有两种"方言",时间和部分,它们有不同的根节点(和不同的结构):
<?xml version="1.0" encoding='UTF-8' standalone='no' ?>
<!DOCTYPE score-partwise PUBLIC "-//Recordare//DTD MusicXML 2.0 Partwise//EN" "http://www.musicxml.org/dtds/partwise.dtd">
<score-partwise version="2.0">
<work>...</work>
...
</score-partwise>
Run Code Online (Sandbox Code Playgroud)
和
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE score-timewise PUBLIC "-//Recordare//DTD MusicXML 2.0 Timewise//EN" "http://www.musicxml.org/dtds/timewise.dtd">
<score-timewise version="2.0">
<work>...</work>
...
</score-timewise>
Run Code Online (Sandbox Code Playgroud)
我到目前为止反序列化分数的代码是:
using (var fileStream = new FileStream(openFileDialog.FileName, FileMode.Open))
{
var xmlSerializer = new XmlSerializer(typeof(ScorePartwise));
var result = (ScorePartwise)xmlSerializer.Deserialize(fileStream);
}
Run Code Online (Sandbox Code Playgroud)
区分两种方言的最佳方法是什么?
我有一个类InputConfig,其中包含List<IncludeExcludeRule>:
public class InputConfig
{
// The rest of the class omitted
private List<IncludeExcludeRule> includeExcludeRules;
public List<IncludeExcludeRule> IncludeExcludeRules
{
get { return includeExcludeRules; }
set { includeExcludeRules = value; }
}
}
public class IncludeExcludeRule
{
// Other members omitted
private int idx;
private string function;
public int Idx
{
get { return idx; }
set { idx = value; }
}
public string Function
{
get { return function; }
set { function = value; …Run Code Online (Sandbox Code Playgroud) 我有两个项目的解决方案; 一个asp.net MVC应用程序和一个类库.我们称他们为项目MVC和项目CLS.
在项目CLS中,有两个不同版本(V1和V2)的XSD文件,我用它来创建两个具有相同名称的可序列化类,但使用xsd2code在不同的命名空间(V1和V2)下.
在MVC项目中,当用户上载XML文件时,CLS.dll用于将XML反序列化为对象.当XML文件是V1类型时,反序列化速度非常快,但V2版本的XSD文件要复杂得多,反序列化可能需要几分钟,而且只是第一次(事后很快) ,直到应用程序再次运行).
我使用Sgen.exe工具CLS.XmlSerializers.dll为CLS.V2类型创建了一个序列化程序程序集(),以便消除动态首次创建程序集,从而提高性能.
我已经成功地将Sgen任务添加到Post Build事件中,并且CLS.XmlSerializers.dll每次构建项目时都会创建程序集.另外,我在这篇文章中使用了单元测试代码来确保程序集已加载,并且确实如此.测试通过成功.
但是,仍然是第一次对XML文件进行反序列化时,需要很长时间.所以,还有一些事情应该是错的.但是,我不知道是什么.请帮忙.
更新:
我Fuslogvw.exe按照评论中的建议使用,我可以看到CLS.XmlSerializers.dll正在成功加载.那么,为什么第一次对XML文件进行反序列化需要大约一分钟,但每次只需不到一秒钟?
更新2:
两个XSD文件之间的区别之一是第二个(V2)引用了一个非常大的XSD文件,该文件包含xs:enumeration主文件中使用的某些类型的定义.而且,这就是反序列化需要很长时间的原因.由于所有我需要做的是将XML文件的反序列化到对象中,并不需要验证对那些枚举的属性和元素的值,我结束了删除该引用到XSD文件,并与更换所有的枚举类型的基类型(在本例中xs:string).现在,V2的反序列化速度与V1一样快,我甚至不需要使用Sgen.exe.我想Sgen.exe只有在你需要反序列化一个非常大的XML文件的情况下才有用.就我而言,XML文件总是很小,但是反序列化很复杂.
xmlserializer ×10
c# ×7
xml ×5
.net ×3
asp.net ×1
attributes ×1
datetime ×1
escaping ×1
java ×1
musicxml ×1
sgen ×1
vb.net ×1
whitespace ×1
xsd ×1