Solved

How do I parse a xml data field from SharePoint?

Posted on 2009-05-05
3
1,384 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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Default parameter problem in C# 3 27
Image(2) 3 29
String manipulation 15 49
XAML: Layout 8 9
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This video teaches viewers about errors in exception handling.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now