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.

2
  • Uh. Btw. The app is pretty much taken from this guide: gregjopa.com/2011/04/… Commented Aug 25, 2011 at 8:55
  • What is your naming convention? For example, are you creating a separate xml file per entity, per user etc.? Commented Aug 25, 2011 at 11:45

2 Answers 2

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,

Sign up to request clarification or add additional context in comments.

3 Comments

I get the NullReferenceException because it completly ignores the second constructor, and thereby nothing is binded to the model.
Glad you found it. If you have more issues with this, you should post a new question I guess.
I made a workaround. I put the code from the constructor into all the CRUD methods. stackoverflow.com/questions/7244014/…
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.