.net - C# - Xml serializing ambiguous complex types -


i have problem can perhaps solved in more efficient or clean way, current solution.

over past days have gotten understanding of how xml serialization can controlled via attributes, 1 part of xml structure obscure, have not been able find smart solution.

i have been handed xml schema, cannot altered, , must create c# class.

the code bugs me following:

<xs:complextype name="datareftype">     <xs:sequence>         <xs:element name="data" type="datatype"/>         <xs:sequence minoccurs="0" maxoccurs="unbounded">             <xs:element name="separator" type="xs:string"/>             <xs:element name="data" type="datatype"/>         </xs:sequence>     </xs:sequence> </xs:complextype> <xs:complextype name="datatype">     <xs:choice>         <xs:sequence>             <xs:element name="dataowner" type="xs:string"/>             <xs:element name="name" type="xs:string"/>         </xs:sequence>         <xs:element name="xpath" type="xs:string"/>     </xs:choice> </xs:complextype> 

my first idea manage having generic list, 1 of 3 types of classes, has worked far, problem here ambiguous name "data".

here code made:

[xmlroot("dataref")] public class dataref {     protected list<dataroot> m_data = new list<dataroot>(1);      [xmlelement(typeof(datauserfield))]     [xmlelement(typeof(datamasterfield))]     [xmlelement(typeof(dataseparator))]     public list<dataroot> data { { return m_data; } set { m_data = value; } } }  public abstract class dataroot { }  [xmlroot("data")] public class datauserfield : dataroot {     protected string m_dataowner;     protected string m_name;      [xmlelement("dataowner")]     public string dataowner { { return m_dataowner; } set { m_dataowner = value; } }     [xmlelement("name")]     public string name { { return m_name; } set { m_name = value; } } }  [xmlroot("data")] public class datamasterfield : dataroot {     protected string m_xpath;      [xmlelement("xpath")]     public string xpath { { return m_xpath; } set { m_xpath = value; } } }  [xmlroot("separator")] public class dataseparator : dataroot {     protected string m_text = "";      [xmltext()]     public string text { { return m_text; } set { m_text = value; } } } 

this unfortunately did not work, , assume because of ambiguous element name "data", since error-message pretty vague, have reference line-number of error.

as said in beginning, have solution, far elegant, , not in .net spirit, why appreciate help.

my solution interested involves 1 class represent "datatype" 1 field type , 3 other fields "dataowner", "name" , "xpath", type enum can either "datauserfield" or "datamasterfield".

sorry long post, felt better have proper code samples, please if need more info.

have @ xsd.exe tool, generate c# classes xsd schema.

it should installed on machine (c:\program files\microsoft sdks\windows\v6.0a\bin\xsd.exe .net framework 3.5).

there's tutorial use on codeproject.com


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -