त्रुटि दिखाने वाली ऑब्जेक्ट की सूची में XML को डिसेरिएलाइज़ करना < xmlns=''> अपेक्षित नहीं था।


मैं अपने एप्लिकेशन से SOAP सेवाओं का उपभोग कर रहा हूं और मुझे अपने कोड में प्रतिक्रिया उपभोग .wsdl फ़ाइल के रूप में निम्नलिखित XMl सामग्री मिलती है।

<Vehicles>
    <FourWheeler>
        <Availability>
            <Brand>Toyota</Brand>
            <Model>Fortuner</Model>
            <Country>Japan</Country>
            <Cost>90000</Cost>           
        </Availability>
        <Availability>
            <Brand>Hyundai</Brand>
            <Model>Elentra</Model>
            <Country>South Korea</Country>
            <Cost>75000</Cost>           
        </Availability>
        <Availability>
            <Brand>Volkswagan</Brand>
            <Model>Polo</Model>
            <Country>Gremany</Country>
            <Cost>95000</Cost>                   
        </Availability>
        <Availability>
            <Brand>Tata</Brand>
            <Model>Nano</Model>
            <Country>25000</Country>
            <Cost></Cost>                    
        </Availability>
    </Outbound>
    <TwoWheeler></TwoWheeler>
</Vehicles>

मैंने अपनी कक्षा को XML को इस प्रकार डिसेरिएलाइज़ करने के लिए डिज़ाइन किया है::

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

using System.Xml.Serialization;
namespace ConsoleApp2
{
    [XmlRoot("Vehicles")]
    public class Vehicles
    {
        [XmlElement("FourWheeler")]
        public FourWheeler FourWheeler
        {
            get;
            set;
        }
    }

    [XmlRoot("FourWheeler")]
    public class FourWheeler
    {
        [XmlElement("Availability")]
        public Vehicles Availability
        {
            get;
            set;
        }
    }

    [XmlRoot("Availability")]
    public class Availability
    {

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

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


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

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

XML को सूची में डीसेरिएलाइज़ करने के लिए मेरा कोड है:

XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Vehicles>));
System.IO.StreamReader sr = new System.IO.StreamReader("E:\\xmlTier.txt");
List<Vehicles> flightavailabilitylst = (List<Vehicles>)xmlSerializer.Deserialize(sr);

मुझे लाइन में त्रुटि मिल रही है

List<Vehicles> flightavailabilitylst = (List<Vehicles>)xmlSerializer.Deserialize(sr);

:

System.InvalidOperationException: 'There is an error in XML document (1, 2).'

InvalidOperationException: <Vehicles xmlns=''> was not expected.

क्या गलत हो रहा है और XML को ऑब्जेक्ट की सूची में बदलने का सही तरीका क्या है???

मैंने क्या प्रयास किया है:

XML में देखने पर, पदानुक्रम वाहन > फोरव्हीलर > उपलब्धता की सूची है… इसलिए मैंने कोड जोड़ा:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Vehicles";
xRoot.IsNullable = true;

इससे मुझे कोई मदद नहीं मिली.

समाधान 1

आपका एक्सएमएल मान्य नहीं है. प्रतिस्थापित करें

</Outbound>
<TwoWheeler></TwoWheeler>

साथ

</FourWheeler>

XML एकल वाहन नोड का प्रतिनिधित्व करता है, न कि उनकी सूची का, इसलिए तदनुसार कोड बदलें

XmlSerializer xmlSerializer = new XmlSerializer(typeof(Vehicles));
System.IO.StreamReader sr = new System.IO.StreamReader( ... );

Vehicles flightavailabilitylst = (Vehicles)xmlSerializer.Deserialize(sr);

इसके अलावा उपलब्धता उपलब्ध वस्तुओं की एक सूची है, एक भी वाहन वस्तु नहीं है इसलिए इसे भी बदल दें

[XmlRoot("FourWheeler")]
public class FourWheeler
{
    [XmlElement("Availability")]
    public List<Availability> Availability
    {
        get;
        set;
    }
}

समाधान 2

आपके “फोरव्हीलर” और “आउटबाउंड” टैग “मिलान नहीं” हैं।

यानी आपका XML अमान्य है.

समाधान 3

<Vehicles>
    <FourWheeler>
        <Availability>
            <Brand>Toyota</Brand>
            <Model>Fortuner</Model>
            <Country>Japan</Country>
            <Cost>90000</Cost>           
        </Availability>
        <Availability>
            <Brand>Hyundai</Brand>
            <Model>Elentra</Model>
            <Country>South Korea</Country>
            <Cost>75000</Cost>           
        </Availability>
        <Availability>
            <Brand>Volkswagan</Brand>
            <Model>Polo</Model>
            <Country>Gremany</Country>
            <Cost>95000</Cost>                   
        </Availability>
        <Availability>
            <Brand>Tata</Brand>
            <Model>Nano</Model>
            <Country>25000</Country>
            <Cost></Cost>                    
        </Availability>
    </FourWheeler>
    <TwoWheeler></TwoWheeler>
</Vehicles>

एक्सएमएल अपडेट किया गया. लेकिन अभी भी वही त्रुटि दिखा रहा है।

コメント

タイトルとURLをコピーしました