1

I am attempting to Deseralize an XDocumnet and am receiving the error: "There is an error in XML Documnet(0, 0)."

XML:

<Machine>
  <Asset>
    <Product>COMPELLENT SC8000,1st,2nd,UPG</Product>
    <OrderNumber>12345678</OrderNumber>
    <ServiceTag>1234567</ServiceTag>
    <ShipDate>2014-02-07T00:00:00</ShipDate>
    <Warranties>
      <Warranty Services="4 Hour On-Site Service">
        <Service>
          <ServiceDescription>4 Hour On-Site Service</ServiceDescription>
          <Provider>UNY</Provider>
          <StartDate>2015-07-31T00:00:00</StartDate>
          <EndDate>2016-07-31T23:59:59</EndDate>
          <Type>EXTENDED</Type>
        </Service>
      </Warranty>
      <Warranty Services="CML - Storage Center Core Base">
        <Service>
          <ServiceDescription>CML - Storage Center Core Base</ServiceDescription>
          <Provider>DELL</Provider>
          <StartDate>2015-07-31T00:00:00</StartDate>
          <EndDate>2016-07-31T23:59:59</EndDate>
          <Type>EXTENDED</Type>
        </Service>
      </Warranty>
      <Warranty Services="Silver Premium Support">
        <Service>
          <ServiceDescription>Silver Premium Support</ServiceDescription>
          <Provider>DELL</Provider>
          <StartDate>2015-07-31T00:00:00</StartDate>
          <EndDate>2016-07-31T23:59:59</EndDate>
          <Type>EXTENDED</Type>
        </Service>
      </Warranty>
    </Warranties>
  </Asset>
</Machine>

Classes:

  public class Service
    {
        [XmlElement("ServiceDescription")]
        public string ServiceDescription { get; set; }
        [XmlElement("Provider")]
        public string Provider { get; set; }
        [XmlElement("StartDate")]
        public string StartDate { get; set; }
        [XmlElement("EndDate")]
        public string EndDate { get; set; }

    }
    [XmlType("Warranty")]
    public class Warranty
    {        
        [XmlElement("Service")]
        public Service objWarranty = new Service();

        [XmlAttribute("Services")]
        public string Services {get; set;}
    }
    public class Asset
    {
        [XmlElement("Product")]
        public string Product { get; set; }
        [XmlElement("OrderNumber")]
        public string OrderNumber { get; set; }
        [XmlElement("ServiceTag")]
        public string ServiceTag { get; set; }
        [XmlElement("ShipDate")]
        public string ShipDate { get; set; }
        [XmlArray("Warranties")]
        public List<Warranty> objWarrantyList = new List<Warranty>();
    }

Function: -- Fails w/Error

private static void XlDesc(XDocument doc)
        {
            XmlSerializer deserializer = new XmlSerializer(typeof(List<Asset>));
            List<Asset> assetlist = (List<Asset>)deserializer.Deserialize(doc.Root.CreateReader());
            foreach (var info in assetlist)
            {
                //ToDo
            }

        }

There might be a better way of doing this. I am rather new to working with Linq and xml files. This XML is created from and existing XML file

Ex:

 var groupByWarrany = xlWarranty.GroupBy(x => x.Service);

                var newDocument = new XDocument(new XElement("Machine", xlBaseInfo.Select(z =>
                    new XElement("Asset",
                        new XElement("Product", z.Product),
                        new XElement("OrderNumber", z.OrderNumber),
                        new XElement("ServiceTag", z.ServiceTag),
                        new XElement("ShipDate", z.ShipDate),
                            (new XElement("Warranties", groupByWarrany.Select(x =>
                                new XElement("Warranty", new XAttribute("Services", x.Key),
                                x.Select(y => new XElement("Service",   
                                new XElement("ServiceDescription", y.Service),
                                new XElement("Provider", y.Provider),
                                new XElement("StartDate", y.StartDate),
                                new XElement("EndDate", y.EndDate),
                                new XElement("Type", y.TypeOfWarranty)
                        )).FirstOrDefault()
                        ))))))));

I was thinking maybe I could skip the whole deseralize and use the classes in the creation of the new XDoc.

jrider
  • 1,571
  • 1
  • 10
  • 26
  • What's the inner exception? Also, XML as posted is missing a closing tag (``) – Dan Field Jan 21 '16 at 18:14
  • 4
    My guess is that that's not your *actual* XML document, given that it has "-" before the root element. (If it is, that's the problem... don't copy/paste from a browser view of an XML file.) – Jon Skeet Jan 21 '16 at 18:14
  • I don't see closing tag. There is no Type field in your Service class. Try to deserialize your objects one by one. – Oxoron Jan 21 '16 at 18:17
  • You're also missing the obligatory xml definition tag: `` I'm not certain if this is enough to break the xml by itself, but it certainly couldn't hurt to add it? – user2366842 Jan 21 '16 at 20:51

2 Answers2

0

test call:

XmlSerializer serializer = new XmlSerializer(typeof(Machine));
using (System.IO.FileStream stream = new System.IO.FileStream(@"C:\Users\Administrator\Desktop\test.xml", System.IO.FileMode.Open))
{
     Machine machine = (Machine)serializer.Deserialize(stream);
     // do stuff with machine
}

classes for xml document

/// <remarks/>
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class Machine
{        /// <remarks/>
    public MachineAsset Asset { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAsset
{
    public string Product { get; set; }

    public uint OrderNumber { get; set; }

    public uint ServiceTag { get; set; }

    public System.DateTime ShipDate { get; set; }

    [System.Xml.Serialization.XmlArrayItemAttribute("Warranty", IsNullable = false)]
    public MachineAssetWarranty[] Warranties { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarranty
{
    public MachineAssetWarrantyService Service { get; set; }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Services { get; set; }
}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class MachineAssetWarrantyService
{
    public string ServiceDescription { get; set; }

    public string Provider { get; set; }

    public System.DateTime StartDate { get; set; }

    public System.DateTime EndDate { get; set; }

    public string Type { get; set; }
}

if you are still experiencing strange behaviours pls post the inner exception

wat
  • 32
  • 5
  • Thank you for the response. Thank you for the correction the the classes. I am still receiving the same error. – jrider Jan 21 '16 at 19:07
  • @user3930238 your warranty class should contain "Services" attribute related field. Something like [XmlAttribute("Services")] public string services{get;set;} – Oxoron Jan 21 '16 at 20:06
  • I corrected the Warranty class as suggested and am receiving the same XML error – jrider Jan 21 '16 at 20:26
  • @user3930238 try to change "[XmlArray("Warranties")] public List objWarrantyList = new List();" to [XmlArray("Warranties"),XmlArrayItem("Warranty")] public Warranty[] objWarrantyList; and change other code due to change List to Array. – Oxoron Jan 22 '16 at 06:25
0

So, we found some errors in class construction already (see comments). In this case you have two variants.

First, deserialize objects step-by-step. Create Service xml (manually or copypaste from your sample) and deserialize it, create Warranty xml and deserialize it, create Asset-without-Warranties-list xml and deserialize it, and, finally, create Asset xml with Warranties and deserialize it. It's easier to find such errors in simple classes and increase difficulty.

Second variant: use xsd utility. You need commands like
xsd.exe yourXml.xml /c
xsd.exe yourXml.xsd /c
(see details in documentation). You'll get yourXml.cs file at the end with all necessary classes. Just fix the array properties, they sometimes are processed incoorectly.

Community
  • 1
  • 1
Oxoron
  • 664
  • 1
  • 7
  • 26