Solved

Put XMLnode values into a details view

Posted on 2010-09-01
16
449 Views
Last Modified: 2012-05-10
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
Comment
Question by:QPR
  • 9
  • 7
16 Comments
 
LVL 27

Expert Comment

by:nmarun
ID: 33583154
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
 
LVL 29

Author Comment

by:QPR
ID: 33590948
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
 
LVL 29

Author Comment

by:QPR
ID: 33592460
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
 
LVL 27

Expert Comment

by:nmarun
ID: 33593224
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
 
LVL 29

Author Comment

by:QPR
ID: 33607651
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
 
LVL 27

Expert Comment

by:nmarun
ID: 33838960
Ok, so what attributes do you want from the above xml?

Arun
0
 
LVL 29

Author Comment

by:QPR
ID: 33844119
ows_Purpose_x0020_of_x0020_Designati
ows_Title
ows_ID
ows_Planning_x0020_Map
ows_Location
ows_Legal_x0020_Description
0
 
LVL 27

Expert Comment

by:nmarun
ID: 33847195
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 27

Expert Comment

by:nmarun
ID: 33869336
Did this work for you?

Arun
0
 
LVL 29

Author Comment

by:QPR
ID: 33871388
will let you know today now I'm back at work
0
 
LVL 29

Author Comment

by:QPR
ID: 33924815
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
 
LVL 27

Accepted Solution

by:
nmarun earned 500 total points
ID: 33926199
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
 
LVL 29

Author Comment

by:QPR
ID: 33961065
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
 
LVL 29

Author Comment

by:QPR
ID: 33961486
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
 
LVL 27

Expert Comment

by:nmarun
ID: 33964179
Try this:

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

Arun

0
 
LVL 29

Author Comment

by:QPR
ID: 33986126
"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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now