0

I will start off with I have read all of the other answers to this question and all of them (albeit good solutions) did not work in my case

I created a c# class from my xsd file with

xsd.exe /c neworder.xsd

It generated a class of 7000+ lines so I'll post relevant parts of it.

using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Xml;
using System.Collections.Generic;


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.foo.com/schemas/w/v1.0")]
[System.Xml.Serialization.XmlRootAttribute("new-order", Namespace = "http://www.foo.com/schemas/w/v1.0")]
public partial class neworder
{

    private List<customertype> customersField;

    private string suppliercodeField;

    private string versionField;

    private static System.Xml.Serialization.XmlSerializer serializer;

    public neworder()
    {
        this.customersField = new List<customertype>();
    }

    [System.Xml.Serialization.XmlArrayAttribute(Order = 0)]
    [System.Xml.Serialization.XmlArrayItemAttribute("customer",IsNullable = false)]
    public List<customertype> customers
    {
        get
        {
            return this.customersField;
        }
        set
        {
            this.customersField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute("supplier-code")]
    public string suppliercode
    {
        get
        {
            return this.suppliercodeField;
        }
        set
        {
            this.suppliercodeField = value;
        }
    }

    [System.Xml.Serialization.XmlAttributeAttribute("version")]
    public string version
    {
        get
        {
            return this.versionField;
        }
        set
        {
            this.versionField = value;
        }
    }

    private static System.Xml.Serialization.XmlSerializer Serializer
    {
        get
        {
            if ((serializer == null))
            {
                serializer = new System.Xml.Serialization.XmlSerializer(typeof(neworder));
            }
            return serializer;
        }
    }
public static neworder Deserialize(string xml)
        {
            System.IO.StringReader stringReader = null;
            try
            {
                stringReader = new System.IO.StringReader(xml);
                return ((neworder)(Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader))));
            }
            finally
            {
                if ((stringReader != null))
                {
                    stringReader.Dispose();
                }
            }
        }

That is just a small snippet, the rest isn't that important right now as I feel like if this gets solved, I can solve the rest.

This is the beginning part of the XML file

<new-order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.foo.com/schema/w/v1.0" version="1.0" supplier-code="FAKECODE" schemaLocation="http://www.foo.com/schemas/w/v1.0/TransmissionEnvelope.xsd">
  <customers>
    <customer w-number="123456" customer-number="12345-12345">
      <client-info>
        <client-name>John Doe</client-name>
        <w-id>433348</w-id>
      </client-info>
      <transferee-name>
        <first>John</first>
        <last>Doe</last>
      </transferee-name>
      <spouse-name>
        <first />
        <last />
      </spouse-name>
      <o-address>
        <street1>123 Fake st</street1>
        <city>Fakeville</city>
        <state>CA</state>
        <postal-code>90210</postal-code>
        <country>USA</country>
      </o-address>
      <d-address>
        <street1 />
        <city>harbour</city>
        <state>VA</state>
        <postal-code>55555</postal-code>
        <country>USA</country>
      </d-address>
      <contact-info>
        <phone>
          <phone-type>CELL</phone-type>
          <phone-number>555-555-5555</phone-number>
        </phone>
        <phone>
          <phone-type>HOME</phone-type>
          <phone-number>555-555-5555</phone-number>
        </phone>
        <phone>
          <phone-type>WORK</phone-type>
          <phone-number />
        </phone>
        <email>johndoe@email.com</email>
        <comments>Just any comments here</comments>
      </contact-info>

    </customer>
  </customers>
</new-order>

I try to deserialize it with the following

    XmlSerializer ser = new XmlSerializer(typeof(neworder));
    neworder feed = (neworder)ser.Deserialize(new FileStream(filePath, FileMode.Open)) ;

The error that I get is the infamous:

There is an error in XML document (1, 2). http://www.foo.com/schema/w/v1.0'> was not expected.

I've read over and over again about making sure the root note as attribute XMLROOT which the above does. And it has the right namespace.

I've tried changing XmlRootAttribute to XmlRoot. Nothing. I've tried removing the namespace and re-doing the class and nothing.

Wrecking my brain on what could be wrong here because everything seems legit.

  • Post the schema and the XML file. – aybe Apr 15 '16 at 16:09
  • Can you post a complete xml file (maybe one that's stripped down)? I was able to stop the exception by adding and `s` to `schema` as such: `xmlns="http://www.foo.com/schemas/w/v1.0"`, but I don't know if there are problems by testing with an "xml file" that is *only* the `` element. – Quantic Apr 15 '16 at 16:10
  • Added XML file. Don't want to post schema because its not **my** schema. What I did do as a temporary workaround (we'll see if it pans for all the test documents) is I created an XSD file using xsd.exe from one of the test documents I have then created the class from that. It works on the one xml document but I am trying multiple. Not the way I want to go but, oh well. – reindeerKurt Apr 15 '16 at 16:25
  • You might want to look at this: http://stackoverflow.com/questions/1556874/user-xmlns-was-not-expected-deserializing-twitter-xml – Jim Wilcox Apr 15 '16 at 16:31
  • I tried both of those. When I add the XMLRoot, it says duplicat root tag so I believe it things that XMLRootAttribute is the same as XMLRoot. Then the second part of that solution didn't work either. – reindeerKurt Apr 15 '16 at 16:57
  • Your XML looks invalid.... some of the nodes are mismatched – Monty Apr 15 '16 at 18:05
  • my fault, I've been messing with changing nodes to try to figure out stuff and I gave wrong XML. Edited should be correct and valid and doesn't seem to be the issue. – reindeerKurt Apr 15 '16 at 18:34
  • I didn't think it would, but to help you I need to use your XML.... I can edit your XML then Deserialize it but that's no good if I have made changes to the XML.... – Monty Apr 15 '16 at 18:51
  • The XmlRootAttribute specified _schemas_ as the middle part of your namespace but _schema_ in the document. They must be the same. (Sorry, SO won't post full namespace). – Suncat2000 Mar 11 '20 at 15:00

1 Answers1

0

Try this.... (for the XML that you have posted)

Using.....

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;

Classes.....

[XmlRoot(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Clientinfo
{
    [XmlElement(ElementName = "client-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Clientname { get; set; }
    [XmlElement(ElementName = "w-id", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Wid { get; set; }
}

[XmlRoot(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Transfereename
{
    [XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string First { get; set; }
    [XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Last { get; set; }
}

[XmlRoot(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Spousename
{
    [XmlElement(ElementName = "first", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string First { get; set; }
    [XmlElement(ElementName = "last", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Last { get; set; }
}

[XmlRoot(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Oaddress
{
    [XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Street1 { get; set; }
    [XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string City { get; set; }
    [XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string State { get; set; }
    [XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Postalcode { get; set; }
    [XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Country { get; set; }
}

[XmlRoot(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Daddress
{
    [XmlElement(ElementName = "street1", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Street1 { get; set; }
    [XmlElement(ElementName = "city", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string City { get; set; }
    [XmlElement(ElementName = "state", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string State { get; set; }
    [XmlElement(ElementName = "postal-code", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Postalcode { get; set; }
    [XmlElement(ElementName = "country", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Country { get; set; }
}

[XmlRoot(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Phone
{
    [XmlElement(ElementName = "phone-type", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Phonetype { get; set; }
    [XmlElement(ElementName = "phone-number", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Phonenumber { get; set; }
}

[XmlRoot(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Contactinfo
{
    [XmlElement(ElementName = "phone", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public List<Phone> Phone { get; set; }
    [XmlElement(ElementName = "email", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Email { get; set; }
    [XmlElement(ElementName = "comments", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public string Comments { get; set; }
}

[XmlRoot(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customer
{
    [XmlElement(ElementName = "client-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Clientinfo Clientinfo { get; set; }
    [XmlElement(ElementName = "transferee-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Transfereename Transfereename { get; set; }
    [XmlElement(ElementName = "spouse-name", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Spousename Spousename { get; set; }
    [XmlElement(ElementName = "o-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Oaddress Oaddress { get; set; }
    [XmlElement(ElementName = "d-address", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Daddress Daddress { get; set; }
    [XmlElement(ElementName = "contact-info", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Contactinfo Contactinfo { get; set; }
    [XmlAttribute(AttributeName = "w-number")]
    public string Wnumber { get; set; }
    [XmlAttribute(AttributeName = "customer-number")]
    public string Customernumber { get; set; }
}

[XmlRoot(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Customers
{
    [XmlElement(ElementName = "customer", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Customer Customer { get; set; }
}

[XmlRoot(ElementName = "new-order", Namespace = "http://www.foo.com/schema/w/v1.0")]
public class Neworder
{
    [XmlElement(ElementName = "customers", Namespace = "http://www.foo.com/schema/w/v1.0")]
    public Customers Customers { get; set; }
    [XmlAttribute(AttributeName = "xsi", Namespace = "http://www.w3.org/2000/xmlns/")]
    public string Xsi { get; set; }
    [XmlAttribute(AttributeName = "xmlns")]
    public string Xmlns { get; set; }
    [XmlAttribute(AttributeName = "version")]
    public string Version { get; set; }
    [XmlAttribute(AttributeName = "supplier-code")]
    public string Suppliercode { get; set; }
    [XmlAttribute(AttributeName = "schemaLocation")]
    public string SchemaLocation { get; set; }
}

Code.....

        string strXML = File.ReadAllText("xml.xml");
        byte[] bufXML = ASCIIEncoding.UTF8.GetBytes(strXML);
        MemoryStream ms1 = new MemoryStream(bufXML);

        // Deserialize to object
        XmlSerializer serializer = new XmlSerializer(typeof(Neworder));
        try
        {
            using (XmlReader reader = new XmlTextReader(ms1))
            {
                Neworder deserializedXML = (Neworder)serializer.Deserialize(reader);

            }// put a break point here and mouse-over deserializedXML….
        }
        catch (Exception ex)
        {
            throw;
        }

I am reading your XML in to a string from a file in the application build folder called xml.xml... you will need to get the XML string from somewhere else or create the xml.xml file and save your XML for the code above to work

Monty
  • 1,534
  • 2
  • 10
  • 12
  • This actually works great! very clean too. Thank you very much. I'm still curious on whats actually wrong with the xsd version of the class too because **in theory**, it should work but I can't figure out why. This **DOES** work though with the XML file. I need to test on multiple samples. – reindeerKurt Apr 15 '16 at 19:13
  • Glad to help.... maybe select this solution as the answer if it has solved your issue... Cheers.... – Monty Apr 15 '16 at 19:24
  • @reindeerKurt I don't know anything about schemas but your `schemaLocation` appears to be malformed. For one it points to a uri not in your namespace list (you pointed it to `http://www.foo.com/schema/w/v1.0` whereas your namespace has `http://www.foo.com/schemas/w/v1.0`. Second, it appears that its supposed to be a space-separated list where your `.xsd` goes after the space, see [here](https://msdn.microsoft.com/en-us/library/ms256100(v=vs.110).aspx), unless you want this possibly: https://msdn.microsoft.com/en-us/library/ms256139(v=vs.110).aspx – Quantic Apr 15 '16 at 19:33