I am attempting to Deseralize an XDocumnet and am receiving the error: "There is an error in XML Documnet(0, 0)."
XML:
<?xml version="1.0"?> -<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> </Warranties> </Asset> 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(); } 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.
No comments:
Post a Comment