【问题标题】:How do I Deserialize Just a String with Namespace?如何使用命名空间反序列化一个字符串?
【发布时间】:2014-01-27 22:34:44
【问题描述】:

XML:

<?xml version="1.0" encoding="utf-8"?><string xmlns="http://www.example.com">Message received</string>

我更喜欢使用这样的扩展方法进行反序列化:

    <Extension()> _
    Public Sub DeserializeFromXML(ByRef objTarget As Object, ByVal strXML As String)

        Dim objXMLSerializer As New System.Xml.Serialization.XmlSerializer(objTarget.GetType())
        Dim objStringReader As New System.IO.StringReader(strXML)
        objTarget = objXMLSerializer.Deserialize(objStringReader)

    End Sub

例子:

Dim strExample As String = String.Empty
strExample.DeserializeFromXML(strXML)

这是因为命名空间而失败(没有它也可以工作)。我无法创建从 String 继承的新类,因为它是不可继承的(为了使用属性来定义命名空间)。如何反序列化带有命名空间的简单 XML 字符串?

【问题讨论】:

    标签: .net xml vb.net


    【解决方案1】:

    我做了类似的事情,但这是使用 C#.NET 完成的。

    请看看这是否适合你。

        public static object DeserializeXmlWithNameSpaceToObject(Type ObjectType, string xml)
        {
            object result = null;
    
            // identify the namespace, without a matching namespace the xml can't be serialized
            string defaultNameSpace = string.Empty;
            XmlDocument document = new XmlDocument();
            document.Load(new StringReader(xml));
            defaultNameSpace = document.DocumentElement.NamespaceURI;
    
            // Set the xml reader settings
            XmlReaderSettings xrs = new XmlReaderSettings();
            xrs.CheckCharacters = false;
    
            // Deserialize the xml into an object
            using (StringReader sr = new StringReader(xml))
            {
                using (XmlReader xr = XmlReader.Create(sr, xrs))
                {
                    System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(ObjectType, defaultNameSpace);
                    result = ser.Deserialize(new StringReader(xml));
                }
            }
    
            // return the object
            return result;
        }
    

    使用online tool 将此 C#.NET 代码转换为 VB.NET。 这是转换后的 VB.NET 代码。

    Public Shared Function DeserializeXmlWithNameSpaceToObject(ObjectType As Type, xml As String) As Object
        Dim result As Object = Nothing
    
        ' identify the namespace, without a matching namespace the xml can't be serialized
        Dim defaultNameSpace As String = String.Empty
        Dim document As New XmlDocument()
        document.Load(New StringReader(xml))
        defaultNameSpace = document.DocumentElement.NamespaceURI
    
        ' Set the xml reader settings
        Dim xrs As New XmlReaderSettings()
        xrs.CheckCharacters = False
    
        ' Deserialize the xml into an object
        Using sr As New StringReader(xml)
            Using xr As XmlReader = XmlReader.Create(sr, xrs)
                Dim ser As New System.Xml.Serialization.XmlSerializer(ObjectType, defaultNameSpace)
                result = ser.Deserialize(New StringReader(xml))
            End Using
        End Using
    
        ' return the object
        Return result
    End Function
    

    【讨论】:

    • 感谢发帖。我能够从这段代码中找出问题所在。这么简单的答案:在序列化器的构造函数上使用 DefaultNamespace 重载。
    猜你喜欢
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-11
    相关资源
    最近更新 更多