Solved

How do I parse a xml data field from SharePoint?

Posted on 2009-05-05
3
1,433 Views
Last Modified: 2013-11-18
Hello,

I'm pretty new to coding and have a question on parsing.  I'm coding a web part for SharePoint in VS 2008 (C#).  I have a  SPSiteDataQuery that returns a field with the following information in it.

<recurrence><rule><firstDayOfWeek>su</firstDayOfWeek><repeat><weekly mo="TRUE" tu="TRUE" we="TRUE" th="TRUE" fr="TRUE" weekFrequency="1" /></repeat><repeatInstances>14</repeatInstances></rule></recurrence>

How would I go about getting the needed information from this?  I believe I will only need to know what days are "True" and then number of repeat instances.

Do I use System.Xml.XmlDocument or System.Xml.Linq.XDocument to take in the XML and assist in parsing?  How would the code for this look?

Thanks in advance for any help.
0
Comment
Question by:MyNameIsKevin
3 Comments
 
LVL 27

Accepted Solution

by:
BigRat earned 500 total points
ID: 24312273
Well I use XmlDocument. Use the load() method to load the source of the document, and after checking for any parse error use selectSingleNode() to select those bits I'm interested in. The select procedure takes an XPath expression, so, for example getting whether Thursdays are repeated one does :-

     node = xmlDoc.selectSingleNode('/recurrence/rule/weekly/@th');

Node will be an empty object (nothing or nil) if the attribute "th" is not on weekly. If it is not an empty object you use the property text to return the textual contents :- node.text.

If you have more than one node in the document, like for example <rule> is repeated, you use selectNodes('/recurrence/rule') to get a list of nodes which you can simply iterate through using a for loop (since the list is an enumerator in C#). You then use selectSingleNode to select parts below that node :-

      nodes = xmlDoc.selectNodes('/recurrence/rule');
     for node in nodes do
          thursday = node.selectSingleNode('weekly/@th');
         ......................

OK?

(Please excuse my C#, I haven't written anything in it for a while yet)
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24314806
Another simple way is to use a System.Data.DataSet, and DataSet.ReadXml, and treat it like regular data.  The schema infer tool doesn't handle some complex XML files, but does a pretty good job with simple XML.
0
 
LVL 26

Expert Comment

by:Anurag Thakur
ID: 24322688
after loading the xml to xmldocument object you can get the week values for monday, tuesday etc as
bool isMonday = Convert.ToBoolean(xmlDoc.SelectSingleNode("//recurrence/rule/weekly").Attributes.GetNamedItem("mo").Value);
similarly you can get the other values and also the weekFrequency

to get repeatInstances you can use
int instances = Convert.ToInt32(xmlDoc.SelectSingleNode("//recurrence/rule/repeatInstances").InnerText);
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In case you ever have to remove a faulty web part from a page , add the following to the end of the page url ?contents=1
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question