小编KF-*_*Dev的帖子

使用序列化从XML文件读入C#类

我有以下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)

c# xml

7
推荐指数
1
解决办法
3万
查看次数

将XML字符串反序列化为类

我试图将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)

c#

7
推荐指数
1
解决办法
1166
查看次数

XML序列化命名空间

我的代码产生的命名空间有问题.我想要的是下面的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)

c# xml xml-namespaces

4
推荐指数
1
解决办法
766
查看次数

标签 统计

c# ×3

xml ×2

xml-namespaces ×1