Opening and Reading an XML file in a document library

So in a recent SharePoint implementation, I was required to allow users to create links in web parts that performed server side functions. Most of the functions were known quantities but they were almost all external systems. Most of the requirements were to just display security trimmed links to Oracle and other external systems. I had envisioned a single sign-on, BDC system but it turned out the client didn't have the infrastructure to support getting the external data in that way.

My solution was to create an Xml file describing the kinds and order of calls for each web part and place all the Xml files in a document library for extraction and processing. That allows future users to pull the file, edit it and replace it as appropriate. I had a single web part with a custom text property set to the name of the Xml file. So the user added this generic web part to as many pages as necessary and set the property to the flavor they wanted to show on that page in that position. Pretty simple.

So I needed to open a file in memory from a document library and operate on it as a normal Xml document. Here is the basic structure of one of the files. There are a lot more options in the structure but this gives you the idea.

XML

XML Document

Easy to read and easy to parse. I'm no whiz at parsing Xml but I built a data object to wrap this structure and it seemed to perform pretty well. Here is the code I used to get the Xml file from the library into an Xml document object. This project required VS 2005 so I had no option for using Linq to get the data. Season to taste. Caveat Emptor.

C#

C#

I've seen another way of doing this but this worked best for my situation. I'd be interested in any pointers from others who have a different tack.

JJ

2 Comments

  • Thanks for the input!

    While the indexof may work to tell if "abc" is in "abcde" it will not tell you that "abc" is equal to "Abcde". It will in fact return a non-zero value and therefore I would miss my match and load the wrong file. The idea is that the name that I'm searching for and the name that I found must match exactly except for character case.

    This was a real example since the client has arrangements of controls on pages for each region/country. Their naming convention required EN-US to be "abc.xml" and EU to be "abc_eu.xml" so you see my dilema. If the "abc_eu.xml" happened to appear before "abc.xml" in the list, I'd catch it and load it inappropriately.

    Still, if you checked the length and as well as the indexof, you'd be assured of a match but that's another operation.

  • Clearly I didn't read to the end of your post. You're absolutely correct. This is a much cleaner and more efficient method. Thanks!

Comments have been disabled for this content.