我最近一直在阅读有关序列化的内容.我已经读过,当我使用XmlSerialization时,我无法序列化对象图.什么是对象图以及为什么我不能简单地序列化它?
我有一个调用记录器,用于记录所有方法调用以及与使用XmlSerializer的方法相关的参数.它适用于大多数调用,但它会为具有IEnumerable
类型参数的所有方法抛出异常.
例如,void MethodWithPlace( Place value )
将序列化,但void MethodWithPlace( IEnumerable<Place> value )
不会.
例外是
System.NotSupportedException:无法序列化接口System.Collections.Generic.IEnumerable`1 [[Place,Test,Version = 0.0.0.0,Culture = neutral]].
我应该怎么做才能使用这些方法IEnumerable
作为其参数之一?
我正在尝试将对象写入Xml字符串并获取该字符串并将其保存到数据库中.但首先我需要得到字符串......
private static readonly Encoding LocalEncoding = Encoding.UTF8;
public static string SaveToString<T> (T settings)
{
Stream stream = null;
TextWriter writer = null;
string settingsString = null;
try
{
stream = new MemoryStream();
var serializer = new XmlSerializer(typeof(T));
writer = new StreamWriter(stream, LocalEncoding);
serializer.Serialize(writer, settings);
var buffer = new byte[stream.Length];
stream.Read(buffer, 0, (int)stream.Length);
settingsString = LocalEncoding.GetString(buffer);
}
catch(Exception ex)
{
// If the action cancels we don't want to throw, just return null.
}
finally
{
if (stream != null) …
Run Code Online (Sandbox Code Playgroud) 我遇到序列化和对象的问题,我可以让它创建所有正确的输出,除了我有一个需要值和属性的元素.这是必需的输出:
<Root>
<Method>Retrieve</Method>
<Options>
<Filter>
<Times>
<TimeFrom>2009-06-17</TimeFrom>
</Times>
<Document type="word">document name</Document>
</Filter>
</Options>
</AdCourierAPI>
Run Code Online (Sandbox Code Playgroud)
我可以构建所有它但无法找到设置Document类型属性的方法,这里是对象类的一部分
[XmlRoot("Root"), Serializable]
public class Root
{
[XmlElement("Method")]
public string method="RetrieveApplications";
[XmlElement("Options")]
public _Options Options;
}
public class _Options
{
[XmlElement("Filter")]
public _Filter Filter;
}
public class _Filter
{
[XmlElement("Times")]
public _Times Times;
[XmlElement("Documents")]
public string Documents;
}
Run Code Online (Sandbox Code Playgroud)
这给了我:
<Document>document name</Document>
Run Code Online (Sandbox Code Playgroud)
而不是:
<Document type="word">document name</Document>
Run Code Online (Sandbox Code Playgroud)
但是我找不到纠正这个的方法,请指教.
谢谢
一个问题让我足以在Stack Overflow上注册.目前,如果我想将Color to XML string序列化为命名颜色,或者#rrggbb
,或者#aarrggbb
,我这样做:
[XmlIgnore()]
public Color color;
[XmlElement(ElementName = "Color")]
public String color_XmlSurrogate
{
get { return MyColorConverter.SetColor(color); }
set { color = MyColorConverter.GetColor(value); }
}
Run Code Online (Sandbox Code Playgroud)
这MyColorConverter
是按照我喜欢的方式进行序列化.但所有这一切感觉就像一个kludge,有额外的领域和所有.有没有办法让它在更少的行中工作,可能将TypeDescriptor与与XML相关的C#属性连接起来?
我正在从头构建一个XML文件,需要知道htmlentities()是否会转换每个可能破坏XML文件的字符(可能还有UTF-8数据)?值将来自twitter/flickr feed,所以我需要确定!
我有一个字符串(来自CDATA元素),其中包含XML的描述.我需要将此字符串解码为一个新的字符串,使用C#正确显示字符
现有字符串:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><myreport xmlns="http://test.com/rules/client"><admin><ordernumber>123</ordernumber><state>NY</state></report></myreport>
Run Code Online (Sandbox Code Playgroud)
字符串通缉:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myreport xmlns="http://test.com/rules/client">
<admin><ordernumber>123</ordernumber><state>NY</state></report></myreport>
Run Code Online (Sandbox Code Playgroud) "人"可能会引用另一个人.
public class Person
{
public string Name;
public Person Friend;
}
Person p1 = new Person();
p1.Name = "John";
Person p2 = new Person();
p2.Name = "Mike";
p1.Friend = p2;
Person[] group = new Person[] { p1, p2 };
XmlSerializer ser = new XmlSerializer(typeof(Person[]));
using (TextWriter tw = new StreamWriter("test.xml"))
ser.Serialize(tw,group );
//above code generates following xml
<ArrayOfPerson>
<Person>
<Name>John</Name>
<Friend>
<Name>Mike</Name>
</Friend>
</Person>
<Person>
<Name>Mike</Name>
</Person>
</ArrayOfPerson>
Run Code Online (Sandbox Code Playgroud)在上面的代码中,同一个'Mike'对象存在于两个地方,因为对同一个对象有两个引用.
我试图序列化一个类的几个数据成员是Nullable对象,这里是一个例子
[XmlAttribute("AccountExpirationDate")]
public Nullable<DateTime> AccountExpirationDate
{
get { return userPrincipal.AccountExpirationDate; }
set { userPrincipal.AccountExpirationDate = value; }
}
Run Code Online (Sandbox Code Playgroud)
但是在运行时我得到了错误
无法序列化System.Nullable`1 [System.DateTime]类型的成员'AccountExpirationDate'.XmlAttribute/XmlText不能用于编码复杂类型.
但是我检查过Nullable是一个SerializableAttribute.我究竟做错了什么?