tags:

views:

315

answers:

3

I have a sharepoint server and I am playing around with it programmatically. One of the applications that I am playing with is Microsoft's Call Center. I queried the list of customers:

if (cList.Title.ToLower().Equals("service requests"))
{
    textBox1.Text += "> Service Requests" + Environment.NewLine;
    foreach (SPListItem item in cList.Items)
    {
     textBox1.Text += string.Format(">> {0}{1}", item.Title, Environment.NewLine);
    }
}

One of the properties in item is XML. Here is value of one:

<z:row
  xmlns:z='#RowsetSchema' ows_ID='1' ows_ContentTypeId='0x0106006324F8B638865542BE98AD18210EB6F4'
  ows_ContentType='Contact' ows_Title='Mouse' ows_Modified='2009-08-12 14:53:50' ows_Created='2009-08-12 14:53:50'
  ows_Author='1073741823;#System Account' ows_Editor='1073741823;#System Account'
  ows_owshiddenversion='1' ows_WorkflowVersion='1' ows__UIVersion='512' ows__UIVersionString='1.0'
  ows_Attachments='0' ows__ModerationStatus='0' ows_LinkTitleNoMenu='Mouse' ows_LinkTitle='Mouse'
  ows_SelectTitle='1' ows_Order='100.000000000000' ows_GUID='{37A91B6B-B645-446A-8E8D-DA8250635DE1}'
  ows_FileRef='1;#Lists/customersList/1_.000' ows_FileDirRef='1;#Lists/customersList'
  ows_Last_x0020_Modified='1;#2009-08-12 14:53:50' ows_Created_x0020_Date='1;#2009-08-12 14:53:50'
  ows_FSObjType='1;#0' ows_PermMask='0x7fffffffffffffff' ows_FileLeafRef='1;#1_.000'
  ows_UniqueId='1;#{28A223E0-100D-49A6-99DA-7947CFC38B18}' ows_ProgId='1;#'
  ows_ScopeId='1;#{79BF21FE-0B9A-43B1-9077-C071B61F5588}' ows__EditMenuTableStart='1_.000'
  ows__EditMenuTableEnd='1' ows_LinkFilenameNoMenu='1_.000' ows_LinkFilename='1_.000'
  ows_ServerUrl='/Lists/customersList/1_.000' ows_EncodedAbsUrl='http://spvm:3333/Lists/customersList/1_.000'
  ows_BaseName='1_' ows_MetaInfo='1;#' ows__Level='1' ows__IsCurrentVersion='1' ows_FirstName='Mickey'
  ows_FullName='Mickey Mouse' ows_Comments='&lt;div&gt;&lt;/div&gt;' ows_ServerRedirected='0'
/>

Can I create an XMLnode or some other sort of XML object so that I can easily parse it and pull certain values (these certainties are unknowns right now, since I am just testing right now)?

Thanks SO!

A: 

I came up with this code that seems to work ok. Since my XML/C# knowledge is limited I would imagine there is a simpler way:

public void DoParse(string value, string elementname)
{
    var split = value.Split((char)39);

    XmlDocument xDoc = new XmlDocument();
    XmlElement xRoot = xDoc.CreateElement(elementname);
    xDoc.AppendChild(xRoot);

    for (var i = 0; i < split.Length - 1; i += 2)
    {
     var attribName = split[i].Replace("=", "").Trim();
     var xAttrib = xDoc.CreateAttribute(attribName);
     xAttrib.Value = split[i + 1];
     xRoot.Attributes.Append(xAttrib);
    }
    xDoc.Save(string.Format("c:\\xmlout_{0}.xml", elementname));
}

Gives me:

<Customer xmlns:z="#RowsetSchema" ows_ID="1" ows_ContentTypeId="0x0106006324F8B638865542BE98AD18210EB6F4" ows_ContentType="Contact" ows_Title="Mouse" ows_Modified="2009-08-12 14:53:50" ows_Created="2009-08-12 14:53:50" ows_Author="1073741823;#System Account" ows_Editor="1073741823;#System Account" ows_owshiddenversion="1" ows_WorkflowVersion="1" ows__UIVersion="512" ows__UIVersionString="1.0" ows_Attachments="0" ows__ModerationStatus="0" ows_LinkTitleNoMenu="Mouse" ows_LinkTitle="Mouse" ows_SelectTitle="1" ows_Order="100.000000000000" ows_GUID="{37A91B6B-B645-446A-8E8D-DA8250635DE1}" ows_FileRef="1;#Lists/customersList/1_.000" ows_FileDirRef="1;#Lists/customersList" ows_Last_x0020_Modified="1;#2009-08-12 14:53:50" ows_Created_x0020_Date="1;#2009-08-12 14:53:50" ows_FSObjType="1;#0" ows_PermMask="0x7fffffffffffffff" ows_FileLeafRef="1;#1_.000" ows_UniqueId="1;#{28A223E0-100D-49A6-99DA-7947CFC38B18}" ows_ProgId="1;#" ows_ScopeId="1;#{79BF21FE-0B9A-43B1-9077-C071B61F5588}" ows__EditMenuTableStart="1_.000" ows__EditMenuTableEnd="1" ows_LinkFilenameNoMenu="1_.000" ows_LinkFilename="1_.000" ows_ServerUrl="/Lists/customersList/1_.000" ows_EncodedAbsUrl="http://spvm:3333/Lists/customersList/1_.000" ows_BaseName="1_" ows_MetaInfo="1;#" ows__Level="1" ows__IsCurrentVersion="1" ows_FirstName="Mickey" ows_FullName="Mickey Mouse" ows_Comments="&amp;lt;div&amp;gt;&amp;lt;/div&amp;gt;" ows_ServerRedirected="0" />

Anyone have some input? Thanks.

Anders
You're effectively writing your own XML parser here, except that you're not doing it right (as XML can get much more complicated - consider character entities, comments etc). Just don't do it - use the existing parsers as suggested in other replies.
Pavel Minaev
Thank you pavel, I will try those out.
Anders
+4  A: 

If the XML is valid you could use XmlDocument.LoadXMl like so:

XmlDocument doc = new XmlDocument();
doc.LoadXml(validxmlstring);
Colin
Hrm, I must have overlooked that one. I was trying to use XPath but was unsuccessful. Probably, like I mentioned, due to my lack of experience with XML parsing in .NET. Thanks!
Anders
+1  A: 

You can do this and it should work fine (although I would use the XML document approach Colin mentions, or even better LINQ). You may also find the LINQ extensions in SharePoint Extensions Lib useful.

However, I'm wondering why you would approach it this way instead of using the SPListItem.Item property? It's much simpler to use and very clear. For example:

var title = listItem["Title"];       // Returns title of item
var desc = listItem["Description"];  // Returns value of description field

The only trap is the unusual case of a list that contains a field with an internal name equal to another field's display name. This will always return the value of the field with the internal name first.

Just curious if you have a requirement to go the XML route.

Alex Angas
No, I do not necessarily have to use XML for this. I just thought it would be the easiest way to consume the raw data.
Anders
Oh, I just remembered why the XML: Inside of the list I get from the server the data that I want to consume is inside of the list's XML property. There are some bits of info that I could pull directly from the list, but most is inside that XML field.
Anders