Sunday, October 5, 2008

Read an XML File inside a SharePoint Feature with simplified XPath using a C# Class

While working with SharePoint Customization using WSS object model, we sometimes need to read XML files. A good way to do it is to use XPath. It is more much usable if you simplify its use by writing a C# Class like the following one :
This class allows you to retrieve a single node value or multiple nodes values :
      public class XMLExplorateur
    {
        protected XPathDocument docNav;
        protected XPathNavigator nav;
        protected XPathNodeIterator xit;
        protected bool initpath = true;
        public XMLExplorateur() { }

        public XMLExplorateur(String path)
        {
            try
            {
                docNav = new XPathDocument(path);
                nav = docNav.CreateNavigator();
            }
            catch
            {
                docNav = null;
                nav = null;
            }
        }
        public bool Init(String path)
        {
            try
            {
                docNav = new XPathDocument(path);
                nav = docNav.CreateNavigator();
            }
            catch
            {
                docNav = null;
                nav = null;
                return false;
            }
            return true;
        }

        public List<string> ValuesOf(String Item)
        {
            List<string> myList = new List<string>();
            if (nav == null) return null;
            String tmp = "descendant::" + Item;
            try
            {
                xit = nav.Select(tmp);
                while (xit.MoveNext())
                {
                    myList.Add(xit.Current.Value);
                }
            }
            catch
            {
                myList = null;
            }
            return myList;
        }

        public String ValueOf(String Item)
        {
            if (nav == null) return "Erreur Navigateur null";
            String tmp = "descendant::" + Item;
            try
            {
                xit = nav.Select(tmp);
                if (xit.MoveNext()) tmp = xit.Current.Value;
                else tmp = "null";
            }
            catch
            {
                tmp = "null";
            }
            return tmp;
        }
    }
And here is the way to use it : I asume the xml file is inside a feature directory and I want to get informations from this file in my feature FeatureActivated method :
                protected XMLExplorateur xe=new XMLExplorateur();
                public override void FeatureActivated(SPFeatureReceiverProperties properties)
                {
                        string[] myDirectoryTable = System.IO.Directory.GetFiles(properties.Definition.RootDirectory + @"\Files");
                        String myFileCompletePath = myDirectoryTable[0];
                        xe.Init(myFileCompletePath);

                        myCustomer.FirstName = xe.ValueOf("Customer/FirstName"));
                        myCustomer.LastName= xe.ValueOf("Customer/LastName");

                        List<string> SalesComments = xe.ValuesOf("Customer/SalesComment");
                        foreach (string aComment in SalesComments)
                        {
                            myCustomer.SalesComments.Add(aComment);
                        }
                }
And of course the used xml file content.
<Customer>
    <FirstName>John</FirstName>
    <LastName>Doe</LastName>
    <LatestPurchasseDate>05/10/2008 13:37</LatestPurchasseDate>
    <SalesComment id="1">To be called back next week</SalesComment>
    <SalesComment id="2">was very interested by Mr. Smith porposal</SalesComment>
</Customer>
You will find MSDN link to study XPath syntax : here.

1 comment:

Jessica Garcia said...

Hi Marc, I've tried to implement your code, however in this instruction:

docNav = new XPathDocument(path); in the Init function I get this error:
The remote server returned an error: (401) Unauthorized.

Do you know why this could be happening?
Thanks