1

I've created an app with CRUD functions on XML documents with repository pattern. I have 4 models (4 xml files) with each a repository class. Before it was just 4 xml documents that were read into a XDocument object in the constructor.

 itemData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Items/item1.xml"));

Now I would like to make the xml file dynamic, so it can read unlimited xmls

So whats the best approach? Making a second constructor and passing in a parameter from the url? Something like this:

        public ItemRepository()
            {
            }

            public ItemRepository(string xml)
            {
             itemData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Items/" + xml + ".xml"));
                 ....
            }

Any other suggestions? Cos i get NullReferenceException with the Model with this.

Kasper Skov
  • 1,954
  • 10
  • 32
  • 53
  • Uh. Btw. The app is pretty much taken from this guide: http://www.gregjopa.com/2011/04/crud-operations-with-xml-data-in-asp-net-mvc-3/ – Kasper Skov Aug 25 '11 at 08:55
  • What is your naming convention? For example, are you creating a separate xml file per entity, per user etc.? – Ben Foster Aug 25 '11 at 11:45

2 Answers2

1

I do not see anything bad with you approach except that the repository might accept directly the complete path to get its xml file. It would be just a bit cleaner way of writting it.

AS your NullReferenceException, only a thorough debug might help you. If your first statement was working , I do not see why the next one shouldn't, at least from the bits of code you have written here.

Good luck to you,

Arthis
  • 2,283
  • 21
  • 32
  • I get the NullReferenceException because it completly ignores the second constructor, and thereby nothing is binded to the model. – Kasper Skov Aug 31 '11 at 07:02
  • Glad you found it. If you have more issues with this, you should post a new question I guess. – Arthis Aug 31 '11 at 07:56
  • I made a workaround. I put the code from the constructor into all the CRUD methods. http://stackoverflow.com/questions/7244014/nullreferenceexception-while-using-xelement – Kasper Skov Aug 31 '11 at 09:10
0

Easiest workaround is to just out the code from the constructor into all the CRUD methods. Here a link for a little more info (and more issues :P) NullReferenceException while using XElement

Community
  • 1
  • 1
Kasper Skov
  • 1,954
  • 10
  • 32
  • 53