Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6118179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:24:23+00:00 2026-05-23T15:24:23+00:00

I have an xml being returned by an extension method. Can some please help

  • 0

I have an xml being returned by an extension method. Can some please help me to use <xsl:for-each> on this xml.

public class CustomObj
{
    //function that gets called from XSLT
    public XPathNodeIterator GetResultTable()
    {
        DataTable table = new DataTable("Table1");

        table.Columns.Add("SourceCity");
        table.Columns.Add("DestinationCity");
        table.Columns.Add("Fare");

        table.Rows.Add(new object[] { "New York", "Las Vegas", "100" });
        table.Rows.Add(new object[] { "New York", "London", "200" });
        table.Rows.Add(new object[] { "New York", "New Delhi", "250" });

        StringWriter writer = new StringWriter();
        table.WriteXml(writer);

        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("Root");
        root.InnerXml = writer.ToString();
        doc.AppendChild(root);

        return doc.CreateNavigator().Select("root");
    }
}

I want to iterate over this xml. Some one please help. I am new to XSLT and would appreciate if you can provide example on the given xml itself.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:24:24+00:00Added an answer on May 23, 2026 at 3:24 pm

    There are two things to note:

    1. It is much more natural to apply the transformation to the result of invoking the method GetResultTable() than to get its result via an extension function.

    2. As written the GetResultTable() method doesn’t return any node at all: in the statement

    —

       return doc.CreateNavigator().Select("root");
    

    the Select() method doesn’t select anything as there is no root element in doc. The element named Root isn’t selected, because XML and XPath are case-sensitive.

    Another observation is that it isn’t necessary at all to use xsl:for-each within an XSLT transformation — this is considered not a good practice in most cases.

    Having said that, here is the complete code for what this question asks for:

    namespace TestXml
    {
        using System;
        using System.Data;
        using System.IO;
        using System.Xml;
        using System.Xml.XPath;
        using System.Xml.Xsl;
    
        class Program
        {
            static void Main(string[] args)
            {
                CustomObj co = new CustomObj();
                XPathNodeIterator xpni = co.GetResultTable();
    
                XslCompiledTransform xslt = new XslCompiledTransform(true);
                xslt.Load(@"..\..\My.xslt");
    
                XsltArgumentList xargs = new XsltArgumentList();
                xargs.AddExtensionObject("my:extension", co);
    
                XmlDocument fakeDoc = new XmlDocument();
                fakeDoc.LoadXml("<t/>");
    
                StringWriter sw = new StringWriter();
    
                xslt.Transform(fakeDoc.CreateNavigator(), xargs, sw);
    
                string result = sw.ToString();
    
                Console.Write(result);
            }
        }
    
        public class CustomObj
        {    //function that gets called from XSLT    
            public XPathNodeIterator GetResultTable()    
            {        
                DataTable table = new DataTable("Table1");        
                table.Columns.Add("SourceCity");        
                table.Columns.Add("DestinationCity");        
                table.Columns.Add("Fare");        
                table.Rows.Add(new object[] { "New York", "Las Vegas", "100" });        
                table.Rows.Add(new object[] { "New York", "London", "200" });        
                table.Rows.Add(new object[] { "New York", "New Delhi", "250" });        
                StringWriter writer = new StringWriter();        
                table.WriteXml(writer);        
                XmlDocument doc = new XmlDocument();        
                XmlElement root = doc.CreateElement("Root");        
                root.InnerXml = writer.ToString();        
                doc.AppendChild(root);        
                return doc.CreateNavigator().Select("Root");    
            }
        }
    }
    

    and the file My.xslt:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:my="my:extension"
     exclude-result-prefixes="my">
      <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
      <xsl:template match="/">
        <html>
          <table border="1">
            <tr>
              <td>Source</td>
              <td>Destination</td>
              <td>Fare</td>
            </tr>
            <xsl:apply-templates select="my:GetResultTable()/*/Table1"/>
          </table>
        </html>
      </xsl:template>
    
      <xsl:template match="Table1">
        <tr>
          <xsl:apply-templates/>
        </tr>
      </xsl:template>
    
      <xsl:template match="Table1/*">
        <td>
          <xsl:apply-templates/>
        </td>
      </xsl:template>
    </xsl:stylesheet>
    

    When the application is executed, the wanted, correct result is produced:

    <html>
      <table border="1">
        <tr>
          <td>Source</td>
          <td>Destination</td>
          <td>Fare</td>
        </tr>
        <tr>
        <td>New York</td>
        <td>Las Vegas</td>
        <td>100</td>
      </tr>
        <tr>
        <td>New York</td>
        <td>London</td>
        <td>200</td>
      </tr>
        <tr>
        <td>New York</td>
        <td>New Delhi</td>
        <td>250</td>
      </tr>
      </table>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some xml being returned in sharepoint, Im using xslt to create the
I have a list of items being returned in xml. each item has various
I have XML that is being returned back from a rest service WCF. The
I have a stream of xml being returned from a webservice which is coming
I have some Json being returned from facebook which I'm then parsing in to
In rails you can use .each do || to loop through the returned results
I have to make a schema for an XML format that's already being used.
I have xml where some of the element values are unicode characters. Is it
I have XML that looks something like this: <Root xmlns=http://widgetspecA.com/ns> ...any... <WidgetBox> <A/> <B/>
I have XML that looks like this: <Parameter Name=parameter name Value=parameter value /> which

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.