none
XML prefix deserialization problems RRS feed

  • Question

  • There is a xml file, where one tag and several attributes has prefix. So I can't get value in this case.

    xml:

    <?xml version="1.0" encoding="utf-16"?>
    <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Body xmlns="urn:types.nmvs.eu:v2.0">
    <Product>
    <ProductCode p6:scheme="AT85" xmlns:p6="urn:types.nmvs.eu:v2.0">03838989662123</ProductCode>
    <Batch>
    <Id>TEST7957</Id>
    <ExpDate>210500</ExpDate>
    </Batch>
    </Product>
    <Pack p5:sn="0DPTT705Y" p5:state="INACTIVE" xmlns:p5="urn:types.nmvs.eu:v2.0">
    <p5:Reason>OUTOFORDER</p5:Reason>
    </Pack>
    </Body>
    </Response>

    classes:

     public class Reason
        {
            [XmlElement]
            public string Value { get; set; }
        }
        public class Batch
        {
            [XmlElement("Id")]
            public string Id { get; set; }

            [XmlElement("ExpDate")]
            public string ExpDate { get; set; }
        }

        public class ProductCode
        {
            [XmlAttribute("Scheme", Namespace = "")]
            public string scheme { get; set; }

            [XmlText]
            public string Value { get; set; }
        }
        public class Product
        {
            [XmlElement("ProductCode")]
            public ProductCode ProductCode { get; set; }

            [XmlElement("Batch")]
            public Batch Batch { get; set; }

            [XmlElement("Reason", Namespace = "urn:types.nmvs.eu:v2.0")]
            public Reason Reason { get; set; }
        }

        public class Body
        {
            [XmlElement("Product")]
            public Product Product { get; set; }
        }

        [XmlRoot("Response")]
        public class Response
        {
            [XmlAttribute("xsd", Namespace = "http://www.w3.org/2001/XMLSchema")]
            public string Xsd { get; set; }

            [XmlElement("Body", Namespace = "urn:types.nmvs.eu:v2.0")]
            public Body Body { get; set; }

        }

    code:

    Response response = new Response();
    XmlSerializer serializer = new XmlSerializer(typeof(Response));
    StreamReader reader = new StreamReader("test.xml");
    response = (Response)serializer.Deserialize(reader);

    Problems with next attributes: p6:scheme="AT85", p5:sn="0DPTT705Y" p5:state="INACTIVE" and with tag: <p5:Reason>OUTOFORDER</p5:Reason>

    Value for this element and attributes in result object is Null. What I need to do, to get this values?

    Thursday, June 6, 2019 11:01 AM

Answers

  • Hi Evzhel,

    Thank you for posting here.

    Based on your description, you want to get values from the xml by using Deserialization.

    I have modified xml and c# code, you could have a look.

    <?xml version="1.0" encoding="utf-16"?>
    <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Body xmlns="urn:types.nmvs.eu:v2.0">
    <Product>
    <ProductCode scheme="AT85" xmlns:p6="urn:types.nmvs.eu:v2.0">03838989662123</ProductCode>
    <Batch>
    <Id>TEST7957</Id>
    <ExpDate>210500</ExpDate>
    </Batch>
    </Product>
    <Pack p5:sn="0DPTT705Y" p5:state="INACTIVE" xmlns:p5="urn:types.nmvs.eu:v2.0">
    <p5:Reason>OUTOFORDER</p5:Reason>
    </Pack>
    </Body>
    </Response>

    Code:

    namespace _3.XML_prefix_deserialization_problems
    {
        [XmlRoot(ElementName = "Response")]
        public class Response
        {
            [XmlAttribute(AttributeName ="xsd",Namespace = "http://www.w3.org/2000/xmlns/")]
            public string Xsd { get; set; }
    
            [XmlElement("Body", Namespace = "urn:types.nmvs.eu:v2.0")]
            public Body Body { get; set; }
    
        }
        public class Reason
        {
            [XmlElement]
            public string Value { get; set; }
        }
        public class Batch
        {
            [XmlElement("Id")]
            public string Id { get; set; }
    
            [XmlElement("ExpDate")]
            public string ExpDate { get; set; }
        }
    
        public class ProductCode
        {
            [XmlAttribute(AttributeName ="scheme",Namespace = "urn:types.nmvs.eu:v2.0")]
            public string scheme { get; set; }
    
            [XmlText]
            public string Value { get; set; }
        }
       
    
        public class Product
        {
            [XmlElement("ProductCode")]
            public ProductCode ProductCode { get; set; }
    
            [XmlElement("Batch")]
            public Batch Batch { get; set; }
    
    
        }
        public class Pack
        {
            [XmlElement("Reason")]
            public string Reason { get; set; }
        }
        public class Body
        {
            [XmlElement("Product")]
            public Product Product { get; set; }
    
            [XmlElement("Pack")]
            public Pack pack { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Response response = new Response();
                XmlSerializer serializer = new XmlSerializer(typeof(Response));
                StreamReader reader = new StreamReader("D:\\t.xml");
                response = (Response)serializer.Deserialize(reader);
    
            }
        }
    }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Evzhel Friday, June 7, 2019 8:09 AM
    Friday, June 7, 2019 7:33 AM
    Moderator

All replies

  • The XmlAttribute for ProductCode specifies Namespace="".  It should be Namespace="urn:types.nmvs.eu:v2.0".  I don't see your Pack class, but its "sn" and "state" memnbers should have the name Namespace value, as should the Reason class.

    Tim Roberts | Driver MVP Emeritus | Providenza &amp; Boekelheide, Inc.

    Thursday, June 6, 2019 5:19 PM
  • Hi Evzhel,

    Thank you for posting here.

    Based on your description, you want to get values from the xml by using Deserialization.

    I have modified xml and c# code, you could have a look.

    <?xml version="1.0" encoding="utf-16"?>
    <Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Body xmlns="urn:types.nmvs.eu:v2.0">
    <Product>
    <ProductCode scheme="AT85" xmlns:p6="urn:types.nmvs.eu:v2.0">03838989662123</ProductCode>
    <Batch>
    <Id>TEST7957</Id>
    <ExpDate>210500</ExpDate>
    </Batch>
    </Product>
    <Pack p5:sn="0DPTT705Y" p5:state="INACTIVE" xmlns:p5="urn:types.nmvs.eu:v2.0">
    <p5:Reason>OUTOFORDER</p5:Reason>
    </Pack>
    </Body>
    </Response>

    Code:

    namespace _3.XML_prefix_deserialization_problems
    {
        [XmlRoot(ElementName = "Response")]
        public class Response
        {
            [XmlAttribute(AttributeName ="xsd",Namespace = "http://www.w3.org/2000/xmlns/")]
            public string Xsd { get; set; }
    
            [XmlElement("Body", Namespace = "urn:types.nmvs.eu:v2.0")]
            public Body Body { get; set; }
    
        }
        public class Reason
        {
            [XmlElement]
            public string Value { get; set; }
        }
        public class Batch
        {
            [XmlElement("Id")]
            public string Id { get; set; }
    
            [XmlElement("ExpDate")]
            public string ExpDate { get; set; }
        }
    
        public class ProductCode
        {
            [XmlAttribute(AttributeName ="scheme",Namespace = "urn:types.nmvs.eu:v2.0")]
            public string scheme { get; set; }
    
            [XmlText]
            public string Value { get; set; }
        }
       
    
        public class Product
        {
            [XmlElement("ProductCode")]
            public ProductCode ProductCode { get; set; }
    
            [XmlElement("Batch")]
            public Batch Batch { get; set; }
    
    
        }
        public class Pack
        {
            [XmlElement("Reason")]
            public string Reason { get; set; }
        }
        public class Body
        {
            [XmlElement("Product")]
            public Product Product { get; set; }
    
            [XmlElement("Pack")]
            public Pack pack { get; set; }
        }
        class Program
        {
            static void Main(string[] args)
            {
                Response response = new Response();
                XmlSerializer serializer = new XmlSerializer(typeof(Response));
                StreamReader reader = new StreamReader("D:\\t.xml");
                response = (Response)serializer.Deserialize(reader);
    
            }
        }
    }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Evzhel Friday, June 7, 2019 8:09 AM
    Friday, June 7, 2019 7:33 AM
    Moderator
  • Thank you for your solution. Unfortunately, the original XML I receive contains “p6: schema” and other similar structures.
    But your solution helps me get the value from "<p5: Reason> OUTOFORDER </ p5: Reason>" in the original xml. So I am a step closer to complete deserialization.

    • Edited by Evzhel Friday, June 7, 2019 8:20 AM
    Friday, June 7, 2019 7:49 AM
  • Hi 

    Thanks for the feedback.

    Based on my search, I only could use this way to get the value that you wanted. I want to know if this solved your problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, June 7, 2019 7:53 AM
    Moderator
  • There is no Pack class because I tried to get date from p6:scheme as step for full deserialization. As the goal was create Pack class as I could get "AT85" for scheme.
    I tried to use "Namespace="urn:types.nmvs.eu:v2.0", but it didn't work. May be I made some logical error in classes structure.
    Friday, June 7, 2019 8:21 AM