C# 反序列化 XML

发布于 2024-11-15 10:43:09 字数 1748 浏览 3 评论 0原文

我在使用 XmlSerializer 类将文档反序列化为对象时遇到问题。

为反序列化编写我的函数:

   static public TYPE xmlToObject<TYPE>( string xmlDoc ) {
        MemoryStream stream = new MemoryStream();
        byte[] xmlObject = ASCIIEncoding.ASCII.GetBytes( xmlDoc );
        stream.Write( xmlObject, 0, xmlObject.Length );
        stream.Position = 0;

        TYPE message;

        XmlSerializer xmlSerializer = new XmlSerializer( typeof( TYPE ) );

        try {
            message = (TYPE)xmlSerializer.Deserialize( stream );
        } catch ( Exception e ) {
            message = default( TYPE );
        } finally {
            stream.Close();
        }
        return message;
    }

我有类:

    public class Test {
           public int a;
           public int b;
    }

并且反序列化:

    string text = File.ReadAllText( "blue1.xml" );
    Test a = XmlInterpreter.xmlToObject<Test>( text );

好的,当我像这样读取文件时:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
       <a>2</a>
       <b>5</b>
    </Test> 

一切正常。但像这样:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
        <a>2</a>
        <b></b>
    </Test> 

是错误的,因为

    <b></b> 

它是空的并且转换为 int 是不可能的。

我该如何解决这个问题?例如,我希望在这种情况下不会声明 b

当我的类是:

    public class Test {

        public enum Pro {
             VALUE1,
             VALUE2
        }

        public Pro p1;
   }

并且我想要接受 xmlDocument,其中字段 p1 为空时。

I have problem with deserialize document to object using XmlSerializer class.

Code my function for deserialize:

   static public TYPE xmlToObject<TYPE>( string xmlDoc ) {
        MemoryStream stream = new MemoryStream();
        byte[] xmlObject = ASCIIEncoding.ASCII.GetBytes( xmlDoc );
        stream.Write( xmlObject, 0, xmlObject.Length );
        stream.Position = 0;

        TYPE message;

        XmlSerializer xmlSerializer = new XmlSerializer( typeof( TYPE ) );

        try {
            message = (TYPE)xmlSerializer.Deserialize( stream );
        } catch ( Exception e ) {
            message = default( TYPE );
        } finally {
            stream.Close();
        }
        return message;
    }

And I have class:

    public class Test {
           public int a;
           public int b;
    }

And deserialize:

    string text = File.ReadAllText( "blue1.xml" );
    Test a = XmlInterpreter.xmlToObject<Test>( text );

Ok, when I read file like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
       <a>2</a>
       <b>5</b>
    </Test> 

everything is OK. But like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <Test>
        <a>2</a>
        <b></b>
    </Test> 

is wrong because

    <b></b> 

is empty and conversion into int is impossible.

How can I solve this? For example I want in this context that b will be not declared.

What when my class is:

    public class Test {

        public enum Pro {
             VALUE1,
             VALUE2
        }

        public Pro p1;
   }

And I want accept xmlDocument, where field p1 is empty.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黑色毁心梦 2024-11-22 10:43:09

我希望第一个示例只是某种类型,因为它也有空的 b 。首先,并不是每个 XML 都可以反序列化为对象。特别是空元素是危险的,不应使用。如果您想表达 b 未定义,则不要将其包含在 XML 文件中:

<?xml version="1.0" encoding="UTF-8"?>
<Test>
    <a>2</a>
</Test> 

或者使您的 b 属性可为空:

public class Test {
       public int a;
       public int? b;
}

并将 XML 定义为:

<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <a>2</a>
    <b xsi:nil="true" />
</Test> 

通常,如果您想使用反序列化,请尝试首先使用序列化了解有效的 XML 必须是什么样子。

I expect that first example is just some type because it has empty b as well. First of all not every XML can be deserialized to object. Especially empty elements are dangerous and should not be used. If you want to express that b is not defined then do not include it in XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Test>
    <a>2</a>
</Test> 

or make your b property nullable:

public class Test {
       public int a;
       public int? b;
}

and define XML as:

<?xml version="1.0" encoding="UTF-8"?>
<Test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <a>2</a>
    <b xsi:nil="true" />
</Test> 

Generally if you want to use deserialization try to first use serialization to understand how must a valid XML look like.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文