CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Feb 2008
    Posts
    2

    XML Namespace conflict issue.

    Hi Friends,

    I have a beautiful article about XML namespace collission here <http://msdn.microsoft.com/en-us/magazine/cc164122.aspx>.

    The answer for the question 1 is suitable for me, except that I have a slightly different situation.

    I have to get the properties of derived class instead of the base.

    Case :
    My B.clsPerson is inheritting A.clsPerson (A and B are namespaces) and I am not supposed to modify A.clsPerson.
    What should be done during this case?
    I still have the conflict as I am not authorized to alter anything inside the 'namespace A'.

    Code:

    namespace A //(NOT AUTHORIZED TO MODIFY)
    {
    public class clsPerson
    {
    public string szName
    {
    get;
    set;
    }

    public string szAddress
    {
    get;
    set;
    }
    }
    }

    namespace B
    {
    public void Serialize(string filename)
    {
    try
    {
    B.clsPerson clsperson = new B.clsPerson();
    clsperson.szAddress = "Address";
    clsperson.szName = "Name";

    lstClsPerson.Add(clsperson);

    clsperson = new B.clsPerson();
    clsperson.szAddress = "Add";
    clsperson.szName = "Eman";

    lstClsPerson.Add(clsperson);

    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(lstClsPerson.GetType());
    StreamWriter streamWriter;
    if (System.IO.File.Exists(filename))
    {
    System.IO.File.Delete(filename);
    }
    streamWriter = System.IO.File.CreateText(filename);
    x.Serialize(streamWriter, lstClsPerson);
    streamWriter.Flush();
    streamWriter.Dispose();
    }
    catch (Exception ex) { MessageBox.Show(ex.InnerException.Message); }
    }

    public class clsPerson : A.clsPerson
    {
    public string szCountry
    {
    get;
    set;
    }
    }
    }

    ERROR

    InnerException = {"Types 'A.clsPerson' and 'B.clsPerson' both use the XML type name, 'clsPerson', from namespace ''. Use XML attributes to specify a unique XML name and/or namespace for the type."}

    What should be done to over come this?

    Regards
    Ansaf Mohammedali

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: XML Namespace conflict issue.

    The error tells you what to do, it says:

    Use XML attributes to specify a unique XML name and/or namespace for the type."}
    Read up on Xml related attributes in msdn: Attributes that control XML Serialization.

  3. #3
    Join Date
    Feb 2008
    Posts
    2

    Re: XML Namespace conflict issue.

    @Arjay: Thanks a lot.
    The solution is a fusion choreography.

    Code:

    namespace B
    {
    public void Serialize(string filename)
    {
    try
    {

    List<clsPerson> lstClsPerson = new List<clsPerson>();

    clsChat chat = new clsChat();

    B.clsPerson clsperson = new B.clsPerson();
    clsperson.szAddress = "Address";
    clsperson.szName = "Name";

    lstClsPerson.Add(clsperson);

    clsperson = new B.clsPerson();
    clsperson.szAddress = "Add";
    clsperson.szName = "Eman";

    lstClsPerson.Add(clsperson);

    chat.lstPerson = lstClsPerson;

    System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(Chat.GetType());
    StreamWriter streamWriter;
    if (System.IO.File.Exists(filename))
    {
    System.IO.File.Delete(filename);
    }
    streamWriter = System.IO.File.CreateText(filename);
    x.Serialize(streamWriter, chat);
    streamWriter.Flush();
    streamWriter.Dispose();
    }
    catch (Exception ex) { MessageBox.Show(ex.InnerException.Message); }
    }

    ///NEW CLASS, INHERITED FROM NAMESPACE 'A'
    [XmlType(TypeName="TheType",AnonymousType=true)]
    public class clsPerson : A.clsPerson
    {
    public string szCountry
    {
    get;
    set;
    }
    }

    ///Introduced a new class.
    public class clsChat
    {

    [XmlArrayItem(Type = typeof(A.clsPerson), ElementName = "Person1"),
    XmlArrayItem(Type = typeof(clsPerson), ElementName = "Person2")]
    public List<clsPerson> lstPerson
    {
    get;set;
    }
    }
    }


    namespace A //(NOT AUTHORIZED TO MODIFY)
    {
    public class clsPerson
    {
    public string szName
    {
    get;
    set;
    }

    public string szAddress
    {
    get;
    set;
    }
    }
    }

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured