XML deserialization stuck - c#

I've generated a xsd file from xml and then priceResponse.cs from xsd file using xsd.exe.
Here is priceResponse.cs code:
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.6.1055.0.
//
/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class document {
private object[] itemsField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute("DATASETS", typeof(documentDATASETS), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("ERROR", typeof(documentERROR), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("PRICE", typeof(documentPRICE), Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public object[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class documentDATASETS {
private string cOUNTRYField;
private string cURRENCYField;
private string pOSTCODEMASKField;
private string tOWNGROUPField;
private string sERVICEField;
private string oPTIONField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string COUNTRY {
get {
return this.cOUNTRYField;
}
set {
this.cOUNTRYField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CURRENCY {
get {
return this.cURRENCYField;
}
set {
this.cURRENCYField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string POSTCODEMASK {
get {
return this.pOSTCODEMASKField;
}
set {
this.pOSTCODEMASKField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string TOWNGROUP {
get {
return this.tOWNGROUPField;
}
set {
this.tOWNGROUPField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SERVICE {
get {
return this.sERVICEField;
}
set {
this.sERVICEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OPTION {
get {
return this.oPTIONField;
}
set {
this.oPTIONField = value;
}
}
}
/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class documentERROR {
private string cODEField;
private string dESCRIPTIONField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CODE {
get {
return this.cODEField;
}
set {
this.cODEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string DESCRIPTION {
get {
return this.dESCRIPTIONField;
}
set {
this.dESCRIPTIONField = value;
}
}
}
/// <uwagi/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class documentPRICE {
private string rATEIDField;
private string sERVICEField;
private string sERVICEDESCField;
private string oPTIONField;
private string oPTIONDESCField;
private string cURRENCYField;
private string rATEField;
private string rESULTField;
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RATEID {
get {
return this.rATEIDField;
}
set {
this.rATEIDField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SERVICE {
get {
return this.sERVICEField;
}
set {
this.sERVICEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string SERVICEDESC {
get {
return this.sERVICEDESCField;
}
set {
this.sERVICEDESCField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OPTION {
get {
return this.oPTIONField;
}
set {
this.oPTIONField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string OPTIONDESC {
get {
return this.oPTIONDESCField;
}
set {
this.oPTIONDESCField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CURRENCY {
get {
return this.cURRENCYField;
}
set {
this.cURRENCYField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RATE {
get {
return this.rATEField;
}
set {
this.rATEField = value;
}
}
/// <uwagi/>
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RESULT {
get {
return this.rESULTField;
}
set {
this.rESULTField = value;
}
}
}
I'm trying to deserialize priceResponse.xml but It seems like it is stuck. I think there is problem with namespaces and I'm doing deserialization wrong.
Here is xml code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<document>
<DATASETS>
<COUNTRY>UTD</COUNTRY>
<CURRENCY>UTD</CURRENCY>
<POSTCODEMASK>UTD</POSTCODEMASK>
<TOWNGROUP>UTD</TOWNGROUP>
<SERVICE>UTD</SERVICE>
<OPTION>UTD</OPTION>
</DATASETS>
<ERROR>
<CODE>P13</CODE>
<DESCRIPTION>RATEID: 1 - Standard Rates</DESCRIPTION>
</ERROR>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>09N</SERVICE>
<SERVICEDESC>9:00 Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>712.93</RATE>
<RESULT>Y</RESULT>
</PRICE>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>10N</SERVICE>
<SERVICEDESC>10:00 Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>706.14</RATE>
<RESULT>Y</RESULT>
</PRICE>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>12N</SERVICE>
<SERVICEDESC>12:00 Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>689.84</RATE>
<RESULT>Y</RESULT>
</PRICE>
<PRICE>
<RATEID>1</RATEID>
<SERVICE>15N</SERVICE>
<SERVICEDESC>Express</SERVICEDESC>
<OPTION>NONE</OPTION>
<OPTIONDESC>NONE</OPTIONDESC>
<CURRENCY>PLN</CURRENCY>
<RATE>670.03</RATE>
<RESULT>Y</RESULT>
</PRICE>
</document>
And finally deserialization code:
File.WriteAllText("priceResponse.xml", x);
//Console.WriteLine(x);
var ser = new XmlSerializer(typeof(document),new XmlRootAttribute("documentPRICE"));
using (var reader = XmlReader.Create("priceResponse.xml"))
{
var wrapper = (document)ser.Deserialize(reader);
foreach (documentPRICE item in wrapper.Items)
{
Console.WriteLine(item.OPTIONDESC);
}
}
I would like to get each items in PRICE element, but I can't deserialize xml. What am I doing wrong ?

In this line of code,
var ser = new XmlSerializer(typeof(document),new XmlRootAttribute("documentPRICE"));
you have serialized the document object but are keeping the root of the xml as documentPRICE. And here,
var wrapper = (document)ser.Deserialize(reader);
you are trying to deserialize it to document object which does not make sense.
The xml created after serializing in your case has documentPRICE as the root object but you are trying to deserialize the same to document object, which is wrong. What you need to do is to keep document as the root while serializing ,as below.
var ser = new XmlSerializer(typeof(document), new XmlRootAttribute("document"));
And it should work.

Related

XML deserialization in C# is returning null array even when XML has data

I'm trying to deserialize an XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:catalogProductListResponse>
<storeView SOAP-ENC:arrayType="ns1:catalogProductEntity[609]" xsi:type="ns1:catalogProductEntityArray">
<item xsi:type="ns1:catalogProductEntity">
<product_id xsi:type="xsd:string">36</product_id>
<sku xsi:type="xsd:string">000975</sku>
<name xsi:type="xsd:string">Beauty Dermo HTM - Vacuoterapia</name>
<set xsi:type="xsd:string">4</set>
<type xsi:type="xsd:string">simple</type>
<category_ids SOAP-ENC:arrayType="xsd:string[9]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">4</item>
</category_ids>
<website_ids SOAP-ENC:arrayType="xsd:string[1]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">1</item>
</website_ids>
</item>
<item xsi:type="ns1:catalogProductEntity">
<product_id xsi:type="xsd:string">37</product_id>
<sku xsi:type="xsd:string">004279</sku>
<name xsi:type="xsd:string">Effect HTM - Radiofrequência</name>
<set xsi:type="xsd:string">4</set>
<type xsi:type="xsd:string">simple</type>
<category_ids SOAP-ENC:arrayType="xsd:string[8]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">4</item>
</category_ids>
<website_ids SOAP-ENC:arrayType="xsd:string[1]" xsi:type="ns1:ArrayOfString">
<item xsi:type="xsd:string">1</item>
</website_ids>
</item>
...[OTHER MANY ITENS]
</storeView>
</ns1:catalogProductListResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
In this class
public class ProductListResponse
{
// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.xmlsoap.org/soap/envelope/", IsNullable = false)]
public partial class Envelope
{
private EnvelopeBody bodyField;
private string encodingStyleField;
/// <remarks/>
public EnvelopeBody Body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified)]
public string encodingStyle
{
get
{
return this.encodingStyleField;
}
set
{
this.encodingStyleField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.xmlsoap.org/soap/envelope/")]
public partial class EnvelopeBody
{
private catalogProductListResponse catalogProductListResponseField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Namespace = "urn:Magento")]
public catalogProductListResponse catalogProductListResponse
{
get
{
return this.catalogProductListResponseField;
}
set
{
this.catalogProductListResponseField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "urn:Magento")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "urn:Magento", IsNullable = false)]
public partial class catalogProductListResponse
{
private storeView storeViewField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("storeView")]
public storeView storeView
{
get
{
return this.storeViewField;
}
set
{
this.storeViewField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "catalogProductEntityArray")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class storeView
{
private storeViewItem[] itemField;
private string arrayTypeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("item")]
public storeViewItem[] item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "catalogProductEntity")]
public partial class storeViewItem
{
private ushort product_idField;
private string skuField;
private string nameField;
private byte setField;
private string typeField;
private storeViewItemCategory_ids category_idsField;
private storeViewItemWebsite_ids website_idsField;
/// <remarks/>
public ushort product_id
{
get
{
return this.product_idField;
}
set
{
this.product_idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public string sku
{
get
{
return this.skuField;
}
set
{
this.skuField = value;
}
}
/// <remarks/>
public string name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
public byte set
{
get
{
return this.setField;
}
set
{
this.setField = value;
}
}
/// <remarks/>
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
public storeViewItemCategory_ids category_ids
{
get
{
return this.category_idsField;
}
set
{
this.category_idsField = value;
}
}
/// <remarks/>
public storeViewItemWebsite_ids website_ids
{
get
{
return this.website_idsField;
}
set
{
this.website_idsField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class storeViewItemCategory_ids
{
private byte[] itemField;
private string arrayTypeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("item")]
public byte[] item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class storeViewItemWebsite_ids
{
private byte itemField;
private string arrayTypeField;
/// <remarks/>
public byte item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute(Form = System.Xml.Schema.XmlSchemaForm.Qualified, Namespace = "http://schemas.xmlsoap.org/soap/encoding/")]
public string arrayType
{
get
{
return this.arrayTypeField;
}
set
{
this.arrayTypeField = value;
}
}
}
}
With the code
var deserializedDocument = SerializacaoHelper.Deserialize<ProductListResponse.Envelope>(xmlDocument);
public static T Deserialize<T>(XmlDocument xmlDoc)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
MemoryStream xmlStream = new MemoryStream();
xmlDoc.Save(xmlStream);
xmlStream.Position = 0;
T obj = (T)serializer.Deserialize(xmlStream);
xmlStream.Flush();
return obj;
}
The class was generated with the Visual Studio automatic generator. To simulate it is due, with the XML in the clipboard, to follow the steps Edit > Paste Special > Paste XML as Classes.
The function that deserializes is being executed, however, the storeView attribute is null, even having child nodes inside it in the XML.
Where is the error? In class? In the deserialization code? I can not understand why the StoreView attribute is null if in XML it has data.

Deserialize XML document to .NET list object

I asked this question yeasterday but all the answers was not what i was looking for and i think it was much because i didnt fomulate the question that good so i will try again.
I have this xml doxument:
<?xml version="1.0" encoding="UTF-8"?>
<people type="array">
<person>
<author-id type="integer">5801</author-id>
<background></background>
<company-id type="integer">81881</company-id>
<created-at type="datetime">2011-08-10T08:39:45Z</created-at>
<first-name>Pr</first-name>
<group-id type="integer" nil="true"></group-id>
<id type="integer">8100134</id>
<last-name>"Cromwell" (Eriksson)</last-name>
<owner-id type="integer" nil="true"></owner-id>
<title>ägare, grafiker</title>
<updated-at type="datetime">2011-08-16T08:17:43Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Studio Total</company-name>
<linkedin-url nil="true"></linkedin-url>
<avatar_url></avatar_url>
<contact-data>
<instant-messengers type="array" />
<phone-numbers type="array">
<phone-number>
<id type="integer">70306123</id>
<location>Work</location>
<number>0703689909</number>
</phone-number>
</phone-numbers>
<email-addresses type="array">
<email-address>
<address>per#studiototal.se</address>
<id type="integer">39720318</id>
<location>Work</location>
</email-address>
</email-addresses>
<twitter-accounts type="array" />
<addresses type="array" />
<web-addresses type="array">
<web-address>
<id type="integer">70124</id>
<location>Work</location>
<url></url>
</web-address>
</web-addresses>
</contact-data>
</person>
<person>
<author-id type="integer">257</author-id>
<background></background>
<company-id type="integer">158696</company-id>
<created-at type="datetime">2013-02-18T12:49:37Z</created-at>
<first-name></first-name>
<group-id type="integer" nil="true"></group-id>
<id type="integer">1538730</id>
<last-name>"Malmer"</last-name>
<owner-id type="integer" nil="true"></owner-id>
<title>Projektledare Online listening</title>
<updated-at type="datetime">2013-02-18T12:49:37Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Santa Maria</company-name>
<linkedin-url nil="true"></linkedin-url>
<avatar_url></avatar_url>
<contact-data>
<instant-messengers type="array" />
<phone-numbers type="array">
<phone-number>
<id type="integer">16649</id>
<location>Work</location>
<number>03-151</number>
</phone-number>
</phone-numbers>
<email-addresses type="array" />
<twitter-accounts type="array" />
<addresses type="array" />
<web-addresses type="array" />
</contact-data>
</person>
<person>
<author-id type="integer">8257</author-id>
<background></background>
<company-id type="integer">1518665</company-id>
<created-at type="datetime">2013-02-01T10:14:27Z</created-at>
<first-name>"Sorush"</first-name>
<group-id type="integer" nil="true"></group-id>
<id type="integer">151827</id>
<last-name></last-name>
<owner-id type="integer" nil="true"></owner-id>
<title></title>
<updated-at type="datetime">2013-02-01T10:16:29Z</updated-at>
<visible-to>Everyone</visible-to>
<company-name>Rancold</company-name>
<linkedin-url nil="true"></linkedin-url>
<avatar_url></avatar_url>
<contact-data>
<instant-messengers type="array" />
<phone-numbers type="array">
<phone-number>
<id type="integer">127275</id>
<location>Work</location>
<number></number>
</phone-number>
</phone-numbers>
<email-addresses type="array">
<email-address>
<address>sjkka#rakkncold.com</address>
<id type="integer">76736018</id>
<location>Work</location>
</email-address>
</email-addresses>
<twitter-accounts type="array" />
<addresses type="array" />
<web-addresses type="array">
<web-address>
<id type="integer">127976</id>
<location>Work</location>
<url>http://www.rancoltyd.com</url>
</web-address>
</web-addresses>
</contact-data>
</person>
<!--there are many more <person> elements-->
</people>
I want to Deserialize this to an .net list object(List<Person> People).
The Person Class:
public class Person
{
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class people
{
private peoplePerson[] personField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("person")]
public peoplePerson[] person
{
get
{
return this.personField;
}
set
{
this.personField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePerson
{
private peoplePersonAuthorid authoridField;
private string backgroundField;
private peoplePersonCompanyid companyidField;
private peoplePersonCreatedat createdatField;
private string firstnameField;
private peoplePersonGroupid groupidField;
private peoplePersonID idField;
private string lastnameField;
private peoplePersonOwnerid owneridField;
private string titleField;
private peoplePersonUpdatedat updatedatField;
private string visibletoField;
private string companynameField;
private peoplePersonLinkedinurl linkedinurlField;
private string avatar_urlField;
private peoplePersonContactdata contactdataField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("author-id")]
public peoplePersonAuthorid authorid
{
get
{
return this.authoridField;
}
set
{
this.authoridField = value;
}
}
/// <remarks/>
public string background
{
get
{
return this.backgroundField;
}
set
{
this.backgroundField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("company-id")]
public peoplePersonCompanyid companyid
{
get
{
return this.companyidField;
}
set
{
this.companyidField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("created-at")]
public peoplePersonCreatedat createdat
{
get
{
return this.createdatField;
}
set
{
this.createdatField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("first-name")]
public string firstname
{
get
{
return this.firstnameField;
}
set
{
this.firstnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("group-id")]
public peoplePersonGroupid groupid
{
get
{
return this.groupidField;
}
set
{
this.groupidField = value;
}
}
/// <remarks/>
public peoplePersonID id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("last-name")]
public string lastname
{
get
{
return this.lastnameField;
}
set
{
this.lastnameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("owner-id")]
public peoplePersonOwnerid ownerid
{
get
{
return this.owneridField;
}
set
{
this.owneridField = value;
}
}
/// <remarks/>
public string title
{
get
{
return this.titleField;
}
set
{
this.titleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("updated-at")]
public peoplePersonUpdatedat updatedat
{
get
{
return this.updatedatField;
}
set
{
this.updatedatField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("visible-to")]
public string visibleto
{
get
{
return this.visibletoField;
}
set
{
this.visibletoField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("company-name")]
public string companyname
{
get
{
return this.companynameField;
}
set
{
this.companynameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("linkedin-url")]
public peoplePersonLinkedinurl linkedinurl
{
get
{
return this.linkedinurlField;
}
set
{
this.linkedinurlField = value;
}
}
/// <remarks/>
public string avatar_url
{
get
{
return this.avatar_urlField;
}
set
{
this.avatar_urlField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("contact-data")]
public peoplePersonContactdata contactdata
{
get
{
return this.contactdataField;
}
set
{
this.contactdataField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonAuthorid
{
private string typeField;
private uint valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public uint Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonCompanyid
{
private string typeField;
private uint valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public uint Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonCreatedat
{
private string typeField;
private System.DateTime valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public System.DateTime Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonGroupid
{
private string typeField;
private bool nilField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool nil
{
get
{
return this.nilField;
}
set
{
this.nilField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonID
{
private string typeField;
private uint valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public uint Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonOwnerid
{
private string typeField;
private bool nilField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool nil
{
get
{
return this.nilField;
}
set
{
this.nilField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonUpdatedat
{
private string typeField;
private System.DateTime valueField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
public System.DateTime Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonLinkedinurl
{
private bool nilField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public bool nil
{
get
{
return this.nilField;
}
set
{
this.nilField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonContactdata
{
private peoplePersonContactdataInstantmessengers instantmessengersField;
private peoplePersonContactdataPhonenumbers phonenumbersField;
private peoplePersonContactdataEmailaddresses emailaddressesField;
private peoplePersonContactdataTwitteraccounts twitteraccountsField;
private peoplePersonContactdataAddresses addressesField;
private peoplePersonContactdataWebaddresses webaddressesField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("instant-messengers")]
public peoplePersonContactdataInstantmessengers instantmessengers
{
get
{
return this.instantmessengersField;
}
set
{
this.instantmessengersField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("phone-numbers")]
public peoplePersonContactdataPhonenumbers phonenumbers
{
get
{
return this.phonenumbersField;
}
set
{
this.phonenumbersField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("email-addresses")]
public peoplePersonContactdataEmailaddresses emailaddresses
{
get
{
return this.emailaddressesField;
}
set
{
this.emailaddressesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("twitter-accounts")]
public peoplePersonContactdataTwitteraccounts twitteraccounts
{
get
{
return this.twitteraccountsField;
}
set
{
this.twitteraccountsField = value;
}
}
/// <remarks/>
public peoplePersonContactdataAddresses addresses
{
get
{
return this.addressesField;
}
set
{
this.addressesField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("web-addresses")]
public peoplePersonContactdataWebaddresses webaddresses
{
get
{
return this.webaddressesField;
}
set
{
this.webaddressesField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonContactdataInstantmessengers
{
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
/// <remarks/>
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class peoplePersonContactdataPhonenumbers
{
private peoplePersonContactdataPhonenumbersPhonenumber phonenumberField;
private string typeField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("phone-number")]
public peoplePersonContactdataPhonenumbersPhonenumber phonenumber
{
get
{
return this.phonenumberField;
}
set
{
this.phonenumberField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
}
}
Error message:
And for those that cant swedish It says after additional information. "There is something wrong in the xml-document (2,2)"
And the "response" variable is the xml string

Adding information to an array within a class

I have converted some xml into classes using xsd and now I am having an issue getting some data into the array that exists in the class to add a list of items. I am trying to get the info into OrderItemsItem.
A bit stumped, any help appreciated (not really keen on making any changes to the classes if I can get away with it, convert to List<> etc) .
This is the code to add the info:
OrderItemsItem orderItemsItem = new OrderItemsItem();
orderItemsItem.CostCentre = "sfsdf";
orderItemsItem.DeliveryDate = "2014-01-05";
orderItemsItem.Fund = "G";
orderItemsItem.ExternalLineRef = "1";
orderItemsItem.ItemName = "dfss";
orderItemsItem.LineNo = "1";
orderItemsItem.ProdId = "dfsf";
orderItemsItem.Project = "";
orderItemsItem.QuantityOrdered = "2";
orderItemsItem.UnitCost = "10";
Order order = new Order();
OrderItemsItem [] items = {orderItemsItem};
order.Items.Item = items;
The Error I am receiving is happening on -> order.Items.Item = items;:
System.NullReferenceException: Object reference not set to an instance
of an object.
This is the Class:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.18444
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Order {
private string referenceField;
private string notesField;
private string orderDateField;
private string statusField;
private OrderItems itemsField;
private OrderBuyerDetails buyerDetailsField;
/// <remarks/>
public string Reference {
get {
return this.referenceField;
}
set {
this.referenceField = value;
}
}
/// <remarks/>
public string Notes {
get {
return this.notesField;
}
set {
this.notesField = value;
}
}
/// <remarks/>
public string OrderDate {
get {
return this.orderDateField;
}
set {
this.orderDateField = value;
}
}
/// <remarks/>
public string Status {
get {
return this.statusField;
}
set {
this.statusField = value;
}
}
/// <remarks/>
public OrderItems Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
/// <remarks/>
public OrderBuyerDetails BuyerDetails {
get {
return this.buyerDetailsField;
}
set {
this.buyerDetailsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItems {
private OrderItemsItem[] itemField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Item")]
public OrderItemsItem[] Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderItemsItem {
private string lineNoField;
private string externalLineRefField;
private string prodIdField;
private string itemNameField;
private string quantityOrderedField;
private string unitCostField;
private string deliveryDateField;
private string costCentreField;
private string projectField;
private string fundField;
/// <remarks/>
public string LineNo {
get {
return this.lineNoField;
}
set {
this.lineNoField = value;
}
}
/// <remarks/>
public string ExternalLineRef {
get {
return this.externalLineRefField;
}
set {
this.externalLineRefField = value;
}
}
/// <remarks/>
public string ProdId {
get {
return this.prodIdField;
}
set {
this.prodIdField = value;
}
}
/// <remarks/>
public string ItemName {
get {
return this.itemNameField;
}
set {
this.itemNameField = value;
}
}
/// <remarks/>
public string QuantityOrdered {
get {
return this.quantityOrderedField;
}
set {
this.quantityOrderedField = value;
}
}
/// <remarks/>
public string UnitCost {
get {
return this.unitCostField;
}
set {
this.unitCostField = value;
}
}
/// <remarks/>
public string DeliveryDate {
get {
return this.deliveryDateField;
}
set {
this.deliveryDateField = value;
}
}
/// <remarks/>
public string CostCentre {
get {
return this.costCentreField;
}
set {
this.costCentreField = value;
}
}
/// <remarks/>
public string Project {
get {
return this.projectField;
}
set {
this.projectField = value;
}
}
/// <remarks/>
public string Fund {
get {
return this.fundField;
}
set {
this.fundField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetails {
private string nameField;
private string emailField;
private OrderBuyerDetailsBillingAddress billingAddressField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
/// <remarks/>
public OrderBuyerDetailsBillingAddress BillingAddress {
get {
return this.billingAddressField;
}
set {
this.billingAddressField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
public partial class OrderBuyerDetailsBillingAddress {
private string nameField;
private string address1Field;
private string address2Field;
private string address3Field;
private string placeField;
private string countyField;
private string postCodeField;
private string countryField;
private string emailField;
/// <remarks/>
public string Name {
get {
return this.nameField;
}
set {
this.nameField = value;
}
}
/// <remarks/>
public string Address1 {
get {
return this.address1Field;
}
set {
this.address1Field = value;
}
}
/// <remarks/>
public string Address2 {
get {
return this.address2Field;
}
set {
this.address2Field = value;
}
}
/// <remarks/>
public string Address3 {
get {
return this.address3Field;
}
set {
this.address3Field = value;
}
}
/// <remarks/>
public string Place {
get {
return this.placeField;
}
set {
this.placeField = value;
}
}
/// <remarks/>
public string County {
get {
return this.countyField;
}
set {
this.countyField = value;
}
}
/// <remarks/>
public string PostCode {
get {
return this.postCodeField;
}
set {
this.postCodeField = value;
}
}
/// <remarks/>
public string Country {
get {
return this.countryField;
}
set {
this.countryField = value;
}
}
/// <remarks/>
public string Email {
get {
return this.emailField;
}
set {
this.emailField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="XXXXXXXXX")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="XXXXXXXXX", IsNullable=false)]
public partial class Post_Printondemand_Create_Full_Order_DataSet {
private Order[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Order")]
public Order[] Items {
get {
return this.itemsField;
}
set {
this.itemsField = value;
}
}
}
Thank you
order.Items.Item = items
The error must be that some order among your orders have its Items collection null (and thus making it not possible to access its "Item" property).
My suspicion is that in your example order.Items is null - you will need to attach a debugger and examine the variables yourself to determine the root cause of your null ref. Ensure that the array is not null before working with it.
in your example you're overriding an element of an array. This will either replace the existing element (if it exists) or (I believe) throw an IndexOutOfBoundsException if it doesn't.
Arrays are generally for fixed lengths of data, ones which you don't append/remove data from frequently. Lists are a better collection to use as they have Add() and Remove() functions for specifically this reason.
It is possible to "append" to C# arrays
order.Items = Order.Items.Union(new Item[]{myNewItem}));
However it's not very nice and creates a new array by merging the old ones! My advice... make the collection a list!

The same table 'tableName' cannot be the child table in two nested relations

Using XSD.EXE I generated a *.cs file to serialize XML. When I serialize I get the error
"The same table 'DefaultFont' cannot be the child table in two nested relations."
Why do I receive the error? Can I fix the error without using XSLT transforms?
Code to serialize
StreamReader fs = new StreamReader(#"C:\path\DocumentSample.xml");
XmlSerializer serializer = new XmlSerializer(typeof(PhysDocDocument));
var result = serializer.Deserialize(fs) as PhysDocDocument;//exception thrown here
Sample XML
<PhysDocDocument xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<PhysDocNode HeaderVisible="true" Display="Summary" IsCollapsed="false" CopyForwardEnabled="true" IsHidden="false" Borders="false">
<LinkedPluginId>00000000-0000-0000-0000-000000000000</LinkedPluginId>
<Plugin xsi:type="PromptPlugin" ID="2446441e-6eb2-49ef-b8e3-28f638755b75">
<CopyForwardChecked>true</CopyForwardChecked>
<PromptBase xsi:type="MemoPrompt">
<Label>Comment</Label>
<DocumentValue>
<Paragraphs />
<DefaultFont FontFamily="Arial" SizeInPoints="10" Style="Regular" />
</DocumentValue>
<ShowLabel>false</ShowLabel>
<RenderHeader>false</RenderHeader>
</PromptBase>
</Plugin>
</PhysDocNode>
</PhysDocDocument>
Generated Code
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.17929
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class PhysDocDocument
{
private PhysDocDocumentPhysDocNode[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PhysDocNode", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNode[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNode
{
private string linkedPluginIdField;
private PhysDocDocumentPhysDocNodePlugin[] pluginField;
private string headerVisibleField;
private string displayField;
private string isCollapsedField;
private string copyForwardEnabledField;
private string isHiddenField;
private string bordersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LinkedPluginId
{
get
{
return this.linkedPluginIdField;
}
set
{
this.linkedPluginIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Plugin", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePlugin[] Plugin
{
get
{
return this.pluginField;
}
set
{
this.pluginField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeaderVisible
{
get
{
return this.headerVisibleField;
}
set
{
this.headerVisibleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Display
{
get
{
return this.displayField;
}
set
{
this.displayField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsCollapsed
{
get
{
return this.isCollapsedField;
}
set
{
this.isCollapsedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string CopyForwardEnabled
{
get
{
return this.copyForwardEnabledField;
}
set
{
this.copyForwardEnabledField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsHidden
{
get
{
return this.isHiddenField;
}
set
{
this.isHiddenField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Borders
{
get
{
return this.bordersField;
}
set
{
this.bordersField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePlugin
{
private string copyForwardCheckedField;
private PhysDocDocumentPhysDocNodePluginPromptBase[] promptBaseField;
private string idField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CopyForwardChecked
{
get
{
return this.copyForwardCheckedField;
}
set
{
this.copyForwardCheckedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PromptBase", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBase[] PromptBase
{
get
{
return this.promptBaseField;
}
set
{
this.promptBaseField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBase
{
private string labelField;
private string showLabelField;
private string renderHeaderField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] documentValueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ShowLabel
{
get
{
return this.showLabelField;
}
set
{
this.showLabelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RenderHeader
{
get
{
return this.renderHeaderField;
}
set
{
this.renderHeaderField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DocumentValue", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] DocumentValue
{
get
{
return this.documentValueField;
}
set
{
this.documentValueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue
{
private string paragraphsField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] defaultFontField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Paragraphs
{
get
{
return this.paragraphsField;
}
set
{
this.paragraphsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DefaultFont", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] DefaultFont
{
get
{
return this.defaultFontField;
}
set
{
this.defaultFontField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont
{
private string fontFamilyField;
private string sizeInPointsField;
private string styleField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FontFamily
{
get
{
return this.fontFamilyField;
}
set
{
this.fontFamilyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SizeInPoints
{
get
{
return this.sizeInPointsField;
}
set
{
this.sizeInPointsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Style
{
get
{
return this.styleField;
}
set
{
this.styleField = value;
}
}
}
Some of your generated classes have AnonymousType=true set in XmlTypeAttribute.
When I modify this and add real class names - the error goes away. Here's full working sample (note that I added 'MemoPrompt' and 'PromptPlugin' class names to XmlTypeAttribute):
using System.Xml.Serialization;
using System.IO;
using System;
class App
{
static void Main()
{
StreamReader fs = new StreamReader(#"sample.xml");
XmlSerializer serializer = new XmlSerializer(typeof(PhysDocDocument));
var result = serializer.Deserialize(fs) as PhysDocDocument;
Console.WriteLine(result);
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class PhysDocDocument
{
private PhysDocDocumentPhysDocNode[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PhysDocNode", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNode[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNode
{
private string linkedPluginIdField;
private PhysDocDocumentPhysDocNodePlugin[] pluginField;
private string headerVisibleField;
private string displayField;
private string isCollapsedField;
private string copyForwardEnabledField;
private string isHiddenField;
private string bordersField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string LinkedPluginId
{
get
{
return this.linkedPluginIdField;
}
set
{
this.linkedPluginIdField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Plugin", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePlugin[] Plugin
{
get
{
return this.pluginField;
}
set
{
this.pluginField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string HeaderVisible
{
get
{
return this.headerVisibleField;
}
set
{
this.headerVisibleField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Display
{
get
{
return this.displayField;
}
set
{
this.displayField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsCollapsed
{
get
{
return this.isCollapsedField;
}
set
{
this.isCollapsedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string CopyForwardEnabled
{
get
{
return this.copyForwardEnabledField;
}
set
{
this.copyForwardEnabledField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string IsHidden
{
get
{
return this.isHiddenField;
}
set
{
this.isHiddenField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Borders
{
get
{
return this.bordersField;
}
set
{
this.bordersField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute("PromptPlugin")]
public partial class PhysDocDocumentPhysDocNodePlugin
{
private string copyForwardCheckedField;
private PhysDocDocumentPhysDocNodePluginPromptBase[] promptBaseField;
private string idField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string CopyForwardChecked
{
get
{
return this.copyForwardCheckedField;
}
set
{
this.copyForwardCheckedField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("PromptBase", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBase[] PromptBase
{
get
{
return this.promptBaseField;
}
set
{
this.promptBaseField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string ID
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute("MemoPrompt")]
public partial class PhysDocDocumentPhysDocNodePluginPromptBase
{
private string labelField;
private string showLabelField;
private string renderHeaderField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] documentValueField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Label
{
get
{
return this.labelField;
}
set
{
this.labelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string ShowLabel
{
get
{
return this.showLabelField;
}
set
{
this.showLabelField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string RenderHeader
{
get
{
return this.renderHeaderField;
}
set
{
this.renderHeaderField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DocumentValue", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue[] DocumentValue
{
get
{
return this.documentValueField;
}
set
{
this.documentValueField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValue
{
private string paragraphsField;
private PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] defaultFontField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string Paragraphs
{
get
{
return this.paragraphsField;
}
set
{
this.paragraphsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("DefaultFont", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont[] DefaultFont
{
get
{
return this.defaultFontField;
}
set
{
this.defaultFontField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class PhysDocDocumentPhysDocNodePluginPromptBaseDocumentValueDefaultFont
{
private string fontFamilyField;
private string sizeInPointsField;
private string styleField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string FontFamily
{
get
{
return this.fontFamilyField;
}
set
{
this.fontFamilyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string SizeInPoints
{
get
{
return this.sizeInPointsField;
}
set
{
this.sizeInPointsField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Style
{
get
{
return this.styleField;
}
set
{
this.styleField = value;
}
}
}

XmlSerialize - Desrialize external XML

I have many files, with the format of:
<?xml version="1.0" encoding="utf-8"?>
<Words>
<word>
<Eng>chain</Eng>
<EngEnd>chained</EngEnd>
<PartOfSpeechEng>verb</PartOfSpeechEng>
<Heb>לקשור עם שרשרת</Heb>
<EngInHeb>צֵ'ין</EngInHeb>
<PartOfSpeechHeb>פועל</PartOfSpeechHeb>
<DicNumber>27</DicNumber>
<Arb>سلسلة</Arb>
<EngInArb>
</EngInArb>
</word>
<word>
<Eng>growl</Eng>
<EngEnd>growls</EngEnd>
<PartOfSpeechEng>verb</PartOfSpeechEng>
<Heb>לנהום</Heb>
<EngInHeb>גְרַאוּל</EngInHeb>
<PartOfSpeechHeb>פועל</PartOfSpeechHeb>
<DicNumber>3</DicNumber>
<Arb>دمدمة/تذمـُّر</Arb>
<EngInArb>
</EngInArb>
</word>
</Words>
I failed to deserialize it into array, can someone help me deserialize it
Thanks
Generated using xsd.exe using schema created by this online tool.
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.1
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
public partial class Words {
private WordsWord[] wordField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("word")]
public WordsWord[] word {
get {
return this.wordField;
}
set {
this.wordField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public partial class WordsWord {
private string engField;
private string engEndField;
private string partOfSpeechEngField;
private string hebField;
private string engInHebField;
private string partOfSpeechHebField;
private int dicNumberField;
private string arbField;
private string engInArbField;
/// <remarks/>
public string Eng {
get {
return this.engField;
}
set {
this.engField = value;
}
}
/// <remarks/>
public string EngEnd {
get {
return this.engEndField;
}
set {
this.engEndField = value;
}
}
/// <remarks/>
public string PartOfSpeechEng {
get {
return this.partOfSpeechEngField;
}
set {
this.partOfSpeechEngField = value;
}
}
/// <remarks/>
public string Heb {
get {
return this.hebField;
}
set {
this.hebField = value;
}
}
/// <remarks/>
public string EngInHeb {
get {
return this.engInHebField;
}
set {
this.engInHebField = value;
}
}
/// <remarks/>
public string PartOfSpeechHeb {
get {
return this.partOfSpeechHebField;
}
set {
this.partOfSpeechHebField = value;
}
}
/// <remarks/>
public int DicNumber {
get {
return this.dicNumberField;
}
set {
this.dicNumberField = value;
}
}
/// <remarks/>
public string Arb {
get {
return this.arbField;
}
set {
this.arbField = value;
}
}
/// <remarks/>
public string EngInArb {
get {
return this.engInArbField;
}
set {
this.engInArbField = value;
}
}
}
and you can use it like this
XmlSerializer mySerializer = new XmlSerializer(typeof(Words));
FileStream myFileStream = new FileStream("path_to_xml",FileMode.Open);
Words words = (Words)mySerializer.Deserialize(myFileStream);

Categories

Resources