Deserializing xml with namespace prefixes that are undefined

The Xml response I get is as follows:

<response>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:com.someDomain.item">
        <name>some name</disc-name>
        <description>some description</disc-desc>
    </item>
    <item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="java:com.someDomain.item">
            <name>some name</disc-name>
            <description>some description</disc-desc>
    </item>
    <otherValue>12.1</otherValue>
</response>

My class is framed as such:

[XmlElement("item")]
public Item[] Items{get;set;}
[XmlElement("otherValue")
public string OtherValue{get;set;}

When I try to deserialize the above Xml into the described class, I get the error message "The namespace prefix" java "is not defined". Adding the "namespace" attribute to the class eliminates the parsing error (however, the xml is then distorted from the original).

t

[XmlElement(ElementName="item",Namespace="java")]

How should I decorate a given property to fit the new namespace? Or, how to define the namespace correctly?

I am not 100% using the stock array for my enumerable section, but I think the namespace problem is currently taking place. Any insights or thoughts are welcome!

UPDATE:

, , :

XmlElementAttribute ( ), , , xsi?

, , , Xml- - , xsi . , ( XmlElement ):

foreach(XmlNode node in element)
node.Attributes.RemoveAll();

, .

+3
2

, XML XML. .

, , , XML , .NET XML CLR.

xsi: , XML .

, , , , - , XML. .

, .

( . : http://norman.walsh.name/2004/01/29/trainwreck)

, : . 1) XML- xsi- , . 2) , .

:

// note this "XmlIncludeAttribute" references the derived type.
// note that in .NET they are in the same namespace, but in XML they are in different namespaces.
[System.Xml.Serialization.XmlIncludeAttribute(typeof(DerivedType))]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://BaseNameSpace")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://BaseNameSpace", IsNullable=true)]
public partial class MyBaseType : object
{
...
}

/// <remarks/>
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://DerivedNameSpace")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://DerivedNameSpace", IsNullable=true)]
public partial class DerivedType: MyBaseType 
{
...
}

, , , . , , , - XML, , .

0

. "java" . . XML. , "java:".

List<Item> Item[].

+1

Source: https://habr.com/ru/post/1713869/


All Articles