我有以下XML文件,我试图使用DE-serialization读入c#中的类:
<?xml version="1.0"?>
<PropertiesMapping>
<Property>
<WEB_Class>InfoRequest</WEB_Class>
<COM_Class>CInfoReq</COM_Class>
<Mappings>
<Map>
<WEB_Property>theId</WEB_Property>
<COM_Property>TheID</COM_Property>
</Map>
<Map>
<WEB_Property>theName</WEB_Property>
<COM_Property>NewName</COM_Property>
</Map>
</Mappings>
</Property>
</PropertiesMapping>
Run Code Online (Sandbox Code Playgroud)
以下是我正在使用的代码,虽然它没有错误地执行,但没有数据被读入类PropertiesMapping,我在哪里错了?
PropertiesMapping pm = null;
try
{
System.IO.StreamReader str = new System.IO.StreamReader(@"PropertyMapping.xml");
System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(PropertiesMapping));
pm = (PropertiesMapping)xSerializer.Deserialize(str);
str.Close();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public class PropertiesMapping
{
private string m_WEB_Class = "";
private string m_COM_Class = "";
private List<IndividualProperties> m_EachProperty = null;
public string …Run Code Online (Sandbox Code Playgroud) 我试图将XML String反序列化到我的类中,该类派生自另一个类,但是我遇到了这样的问题,我收到以下错误:
{"The specified type is abstract: name='DeviceRequest', namespace='', at <DeviceRequest xmlns=''>."}
我假设我缺少一些decorator属性,它会告诉序列化程序如何将这个xml反序列化到类中?
这是我的代码:
//Usage
DeviceRequest dreq = DeviceRequest.ParseRequest(e.XML);
//Base Class
public abstract class IFSFRequestBase
{
private string m_ApplicationSender = "";
private int m_WorkStationID = 0;
private string m_RequestID = "";
[XmlAttribute()]
public abstract string RequestType { get; set; }
public abstract byte[] Message();
[XmlAttribute()]
public string ApplicationSender
{
get
{
return m_ApplicationSender;
}
set
{
m_ApplicationSender = value;
}
}
[XmlAttribute()]
public int WorkStationID
{
get
{
return m_WorkStationID; …Run Code Online (Sandbox Code Playgroud) 我的代码产生的命名空间有问题.我想要的是下面的XML:
<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize Type="Customer" Name="Some Name" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd"
xmlns:Test="http://www.Test.org/" xmlns="http://www.nrf-arts.org/namespace">
<Address>
<Line1>Addr1</Line1>
<Line2>Addr2</Line2>
</Address>
</ClassToSerialize>
Run Code Online (Sandbox Code Playgroud)
我得到的是这个XML:
<?xml version="1.0" encoding="utf-8"?>
<ClassToSerialize xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="http://www.123.org/namespace C:\Schema\ClassToSerialize.xsd"
xmlns:Test="http://www.Test.org/" xmlns:xmlns="http://www.nrf-arts.org/namespace" Type="Customer" Name="Some Name">
<Address>
<Line1>Addr1</Line1>
<Line2>Addr2</Line2>
</Address>
</ClassToSerialize>
Run Code Online (Sandbox Code Playgroud)
主要区别是:
1. xmlns:schemaLocation= needs to be xsi:schemaLocation=
2. xmlns:xmlns= needs to be xmlns=
3. Attributes Order, I would prefer the Attributes to be presented before the namespace attributes (This is not a big Issue, just nice to have)
Run Code Online (Sandbox Code Playgroud)
目前我正在做的事情是以上我想要的值替换序列化的字符串值在1和2,一个可行的技巧,但我怀疑有修改的命名空间代码在这一点上做到这一点的方法吗?
这是我正在使用的代码,如何更改GetNameSpace()以在第1点和第2点执行我需要的操作,还可以重新排序属性吗?:
public partial class Form1 …Run Code Online (Sandbox Code Playgroud)