none
C# XML element named system creating errors in the code RRS feed

  • Question

  • Hi I am very new to C# and have very little other development experience so please don't beat me up too bad. Also please talk on a 5th grade level I am very inexperienced.

    I have an XSD generated class for the XML file I am trying to read with that class. The problem is this XML has a root node named "System" this is causing errors in the code generated by XSD. Is there any way I can encapsulate that "System" from the protected word System?

    Thank you in advance for any advice you can offer me.

    Paul

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    // 
    // This source code was auto-generated by xsd, Version=4.8.3928.0.
    // 
    using System.Xml.Serialization;
    
    namespace XML_Test {
        
     
        /// <remarks/>
        [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.8.3928.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 System {
            
            private SystemFileHeader fileHeaderField;
            
            private SystemPhysicalTree physicalTreeField;
            
            private SystemGeographicTree geographicTreeField;
            
            private string typeField;
            
            private decimal versionField;
            
            /// <remarks/>
            public SystemFileHeader FileHeader {
                get {
                    return this.fileHeaderField;
                }
                set {
                    this.fileHeaderField = value;
                }
            }
            
            /// <remarks/>
            public SystemPhysicalTree PhysicalTree {
                get {
                    return this.physicalTreeField;
                }
                set {
                    this.physicalTreeField = value;
                }
            }
            
     
    Thursday, March 19, 2020 11:55 PM

All replies

  • Hi PablitoCarmelito,

    Thank you for posting here.

    This situation is not uncommon, in order to deal with this kind of problem, we can use [XmlRootAttribute ()].

    By default, the system will automatically add this: [System.Xml.Serialization.XmlRootAttribute (Namespace = "", IsNullable = false)], we can delete it and replace it with [XmlRoot (ElementName = "System")],.

    Then change the class name at will.

    Then we can use XmlSerializer to get the contents of the xml file.

                string path = @"XMLFile1.xml";
               
                XmlSerializer serializer = new XmlSerializer(typeof(System1));
                System1 res = (System1)serializer.Deserialize(new XmlTextReader(path));
    

    Hope this could be helpful.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 20, 2020 8:47 AM
  • Sometimes you can avoid the error specifying the full name ‘XML_Test.System’ instead of ‘System’ and ‘using XML_Test’ in other parts of your code.

    Show some details about the errors.

    • Edited by Viorel_MVP Friday, March 20, 2020 11:03 AM
    Friday, March 20, 2020 10:55 AM
  • Hi,

    Has your issue been resolved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 24, 2020 9:53 AM