• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 500
  • Last Modified:

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

0
QPR
Asked:
QPR
  • 9
  • 7
1 Solution
 
nmarunCommented:
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
0
 
QPRAuthor Commented:
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?
0
 
QPRAuthor Commented:
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

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
nmarunCommented:
string xmlString = nodes.InnerXml;

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

Arun
0
 
QPRAuthor Commented:
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

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

Arun
0
 
QPRAuthor Commented:
ows_Purpose_x0020_of_x0020_Designati
ows_Title
ows_ID
ows_Planning_x0020_Map
ows_Location
ows_Legal_x0020_Description
0
 
nmarunCommented:
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

0
 
nmarunCommented:
Did this work for you?

Arun
0
 
QPRAuthor Commented:
will let you know today now I'm back at work
0
 
QPRAuthor Commented:
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

0
 
nmarunCommented:
The method ReadFromSharePoint should reside inside a class, in this case the FromGIS.aspx.cs class. Plus the example I gave you works only for a Console application and not for a web application. Here's how you can modify your application to make it work.

On the FromGIS.aspx design page, create a grid control something like below.

<asp:DataGrid ID="myGrid" runat="server" AutoGenerateColumns="true" />

Add a .cs file named 'RowData.cs' file to the project and copy the class's definition into this file.

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; }
}

Modify your FromGIS.aspx.cs to the below and then the data from the xml file should get rendered as a grid (table) on the page.

public partial class FromGIS : System.Web.UI.Page
{
        protected void Page_Load(object sender, EventArgs e)
    {
         List<RowData> dataRowsList = ReadSharePointXml();
         myGrid.DataSource = dataRowsList;
         myGrid.DataBind();    
       }

    private List<RowData> ReadSharePointXml()
    {
        // make sure you set the xmlFilePath accordingly
        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();
         return rowDataList;
         }

}

0
 
QPRAuthor Commented:
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)
0
 
QPRAuthor Commented:
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?
0
 
nmarunCommented:
Try this:

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

Arun

0
 
QPRAuthor Commented:
"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!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now