Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

XmlSerializer C#

  • 16-12-2009 1:16pm
    #1
    Registered Users, Registered Users 2 Posts: 7,987 ✭✭✭


    I have tried a few ways to get around this problem still getting the same error.

    It is only a prototype which I am trying to plug into a Windows Service.

    I am looping through a folder looking for XML files and then deserlializing the xml to my class but keep getting the following

    WindowsApplication2.document is inaccessible due to its protection level. Only public types can be processed.

    Can anyone help with error please.
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace WindowsApplication2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                string _sourceFolder = @"C:\folder\IN";
    
                if (Directory.Exists(_sourceFolder))
                {
                    string[] _theFiles = Directory.GetFiles(_sourceFolder);
    
                    foreach (string _file in _theFiles)
                    {  
                        process(_theFiles);
                    }
                }
            }
    
            private void process(string[] _theFiles)
            {
                foreach (string _file in _theFiles)
                {
                    bool _processedOK = false;
    
                    try
                    {
                        document _FN_Document = load(_file);
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
    
            private document load(string _fileName)
            {
    
                document _doc = new document();
                //Type _doc = typeof(document);
                try
                {
                    
    
                    StreamReader _streamReader = new StreamReader(_fileName);
    
                    XmlSerializer _xmlSerializer = new XmlSerializer(typeof(document) );
                    //XmlSerializer _xmlSerializer = new XmlSerializer(_doc.GetType() );
                    _doc = (document)_xmlSerializer.Deserialize(_streamReader);
    
                    _streamReader.Close();
                }
                catch (IOException ex)
                {
                    tracer.WriteTrace(ex.Message);
                }
                catch (Exception ex)
                {
                    eventLogger.LogIt(ex);
                    tracer.WriteTrace("[Error] " + ex.Message);
                }
    
                return _doc;
            }
        }
    }
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace WindowsApplication2
    {
        class document
        {
            private string _documentID;
            private string _returnLocation;
            private int _status;
            private int _requestID;
            private string _emailAddress;
    
            #region "Properties"
    
            public string emailAddress
            {
                get { return this._emailAddress; }
                set { this._emailAddress = value; }
            }
    
            public int requestID
            {
                get { return this._requestID; }
                set { this._requestID = value; }
            }
    
            public string documentID
            {
                get { return this._documentID; }
                set { this._documentID = value; }
            }
    
            public string returnLocation
            {
                get { return this._returnLocation; }
                set { this._returnLocation = value; }
            }
    
            public int status
            {
                get { return this._status; }
                set { this._status = value; }
            }
    
            #endregion
        }
    }
    


Comments

  • Registered Users, Registered Users 2 Posts: 515 ✭✭✭NeverSayDie


    You haven't specified an access level for your "document" class, and I'm guessing it defaults to private/protected or some such, which will cause problems using it from other places (i.e. your Form class).

    Edit; actually the default seems to be "internal", which means anything outside the same assembly won't be able to access it. See here;
    http://msdn.microsoft.com/en-us/library/ms173121.aspx


  • Registered Users, Registered Users 2 Posts: 7,468 ✭✭✭Evil Phil


    You need to declare document as:
    [B]public[/B] class document { 
    }
    

    The default access specifier for a class is internal so your form can use the document class as its within the same namespace, WindowsApplication2. The XmlSerializer is outside of that namespace hence document has to be public for XmlSerializer to see it.


  • Registered Users, Registered Users 2 Posts: 7,987 ✭✭✭Trampas


    Thanks lads.

    Just moving over from VB to C which isn't as bad as I thought.

    Just a few tiny issues like this.


Advertisement