Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I parse a xml data field from SharePoint?

Posted on 2009-05-05
3
Medium Priority
?
1,519 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 27

Accepted Solution

by:
BigRat earned 1500 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

704 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