Link to home
Start Free TrialLog in
Avatar of MyNameIsKevin
MyNameIsKevin

asked on

How do I parse a xml data field from SharePoint?

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.
ASKER CERTIFIED SOLUTION
Avatar of BigRat
BigRat
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Bob Learned
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.
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);