Solved

How do I parse a xml data field from SharePoint?

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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...
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

751 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