Solved

How do I parse a xml data field from SharePoint?

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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Having just graduated from college and entered the workforce, I don’t find myself always using the tools and programs I grew accustomed to over the past four years. However, there is one program I continually find myself reverting back to…R.   So …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

810 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