Link to home
Start Free TrialLog in
Avatar of QPR
QPRFlag for New Zealand

asked on

Put XMLnode values into a details view

I've been doing a lot of work with (sharepoint) web services and have been populating DIVs and drop down lists with Sharepoint list items

I'd like to be able to populate a detailsview with the returned rows but am unsure how
I have an empty detailsview on my page with no datasource and would like to populate fields in it from each row.

foreach (System.Xml.XmlNode node in nodes)
            {
                if (node.Name == "rs:data")
                {
                    for (int i = 0; i < node.ChildNodes.Count; i++)
                    {
                        if (node.ChildNodes[i].Name == "z:row")
                        {
                            //populate detailsview fields
                        }
                    }
                }
            }

Open in new window

Avatar of nmarun
nmarun
Flag of India image

What does your XML look like? May be you'll be better off if you parse the xml using LINQ or something and then just bind a custom collection.

Arun
Avatar of QPR

ASKER

Who knows what it looks like it is built on the fly coming from sharepoint web services so I never actually have a document that I can open. I assume it only exists in memory. I can tell you the column names if that helps?
Any clues on how to do what you suggest? A sample xml block and some custom code to parse/populate?
Avatar of QPR

ASKER

below is what I've come up with so far... no errors but no details view being created at the client
System.Xml.XmlNode nodes = myservice.GetListItems(listName, null, query, viewFields, rowLimit, null, null);

            XmlNodeReader listReader = new XmlNodeReader(nodes);
            DataSet listDS = new DataSet();
            listDS.ReadXml(listReader, XmlReadMode.Auto);
            if (listDS.Tables.Count > 1)
            {
                DetailsView1.DataSource = listDS.Tables[1];
                DetailsView1.DataBind();
            }

Open in new window

string xmlString = nodes.InnerXml;

This should give you the xml returned by the service. Paste it and I'll take it from there.

Arun
Avatar of QPR

ASKER

thanks.
I deliberatly chose one that only had 1 record count for read-ability. They can have up to 35 records which is why I'd like to use a details view
<rs:data ItemCount="1" xmlns:rs="urn:schemas-microsoft-com:rowset">
  <z:row ows_Purpose_x0020_of_x0020_Designati="Meteorological Activties" ows_MetaInfo="203;#" ows__ModerationStatus="0" ows__Level="1" ows_Title="Meteorological Service" ows_ID="203" ows_owshiddenversion="4" ows_UniqueId="203;#{4E348AC0-A85E-4ADC-95B1-A6896A14A8AC}" ows_FSObjType="203;#0" ows_Created="2010-07-13 15:02:11" ows_Planning_x0020_Map="U32" ows_Location="Airport Road" ows_Legal_x0020_Description="Sec 497 SO 25965" ows_FileRef="203;#DP/Lists/Designations/203_.000" xmlns:z="#RowsetSchema" /> 
  </rs:data>

Open in new window

Ok, so what attributes do you want from the above xml?

Arun
Avatar of QPR

ASKER

ows_Purpose_x0020_of_x0020_Designati
ows_Title
ows_ID
ows_Planning_x0020_Map
ows_Location
ows_Legal_x0020_Description
Here's how I would do it.

* Create a class that holds the properties of what you want to read from the xml (see class definition RowData)
* Use LINQ to extract the values from the xml file

Arun

public class RowData
{
    public string Designati { get; set; }
    public string Title { get; set; }
    public string Id { get; set; }
    public string Map { get; set; }
    public string Location { get; set; }
    public string Description { get; set; }
}

private static void ReadSharePointXml()
{
    XDocument xDocument = XDocument.Load(xmlFilePath);
    XNamespace xNamespace = "#RowsetSchema";
    List<RowData> rowDataList = (from listId in xDocument.Descendants(xNamespace + "row")
                                select new RowData
                                        {
                                            Designati = listId.Attribute("ows_Purpose_x0020_of_x0020_Designati").Value,
                                            Description = listId.Attribute("ows_Legal_x0020_Description").Value,
                                            Title = listId.Attribute("ows_Title").Value,
                                            Id = listId.Attribute("ows_ID").Value,
                                            Map = listId.Attribute("ows_Planning_x0020_Map").Value,
                                            Location = listId.Attribute("ows_Location").Value,
                                        }).ToList();

    for (int i = 0; i < rowDataList.Count; i++)
    {
        Console.WriteLine(rowDataList[i].Description);
        Console.WriteLine(rowDataList[i].Designati);
        Console.WriteLine(rowDataList[i].Id);
        Console.WriteLine(rowDataList[i].Map);
        Console.WriteLine(rowDataList[i].Location);
        Console.WriteLine(rowDataList[i].Title);
    }
}

Open in new window

Did this work for you?

Arun
Avatar of QPR

ASKER

will let you know today now I'm back at work
Avatar of QPR

ASKER

ok finally got a chance to look at this.
Created a new page in my solution and pasted your code in the code behind page. Full code shown below in code window.

I'm getting some errors.
private static void ReadSharePointXml()
For void it says... Expected class, delegate, enum, interface, or struct
Same error under select new RowData

rowDataList[i].
all have errors saying "i" could not be found.

this is going into a web app so won't be able to test with console.writelines

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class FromGIS : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

public class RowData
{
    public string Designati { get; set; }
    public string Title { get; set; }
    public string Id { get; set; }
    public string Map { get; set; }
    public string Location { get; set; }
    public string Description { get; set; }
}

private static void ReadSharePointXml()
{
    XDocument xDocument = XDocument.Load(xmlFilePath);
    XNamespace xNamespace = "#RowsetSchema";
    List<RowData> rowDataList = (from listId in xDocument.Descendants(xNamespace + "row")
                                select new RowData
                                        {
                                            Designati = listId.Attribute("ows_Purpose_x0020_of_x0020_Designati").Value,
                                            Description = listId.Attribute("ows_Legal_x0020_Description").Value,
                                            Title = listId.Attribute("ows_Title").Value,
                                            Id = listId.Attribute("ows_ID").Value,
                                            Map = listId.Attribute("ows_Planning_x0020_Map").Value,
                                            Location = listId.Attribute("ows_Location").Value,
                                        }).ToList();

    for (int i = 0; i < rowDataList.Count; i++)
    {
        Console.WriteLine(rowDataList[i].Description);
        Console.WriteLine(rowDataList[i].Designati);
        Console.WriteLine(rowDataList[i].Id);
        Console.WriteLine(rowDataList[i].Map);
        Console.WriteLine(rowDataList[i].Location);
        Console.WriteLine(rowDataList[i].Title);
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of nmarun
nmarun
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of QPR

ASKER

thanks, I have some errors in the select new RowData block
RowData does not contain a definition for Designa
This is the same for title, id, map, location
Do I need to make a reference to this rowdata.cs file (I created it in the app_code folder)
Avatar of QPR

ASKER

my mistake, I made a typo... it works now.
But, I don't have an actual file path to the XML, the XML is returned to me from a web service. How can I reference this stream(?) rather than a physical file?
Try this:

string xml = "your xml as a string from web service";  
TextReader tr = new StringReader(xml);  
XDocument xmlDoc = XDocument.Load(tr);  

Arun

Avatar of QPR

ASKER

"your xml as a string from web service"
What I have is a url to a ashx file that, when served, retuns a web page, viewing the page source shows the xml structure. It is this that I need to navigate and pluck values from.

Actually now that I read up through the posts here I see I've been merging 2 of my questions into 1!
Been struggling with 2 different projects and ended up getting them muddled when posting here sorry.
What I'll do is award the points as you have shown me something very useful that will fix project 1 but I'll post another question and make sure I post the correct details this time... luckily this time sharepoint web services aren't involved!