Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 207
  • Last Modified:

XML C# help

I am using Visual Studios and c#, i have quried a database and got the output that i need in an xml format.
What I want to do is style this format, could someone please help how do i go about doing this.
I have created a xsl style sheet but how do i link it to my project and where.
0
R8VI
Asked:
R8VI
  • 3
  • 2
1 Solution
 
Gautham JanardhanCommented:
u can populate it into a dataset and then call the Dataset.WriteXML function().
0
 
R8VIAuthor Commented:
Thank You for that, is there a tutorial that I can follow please
0
 
Gautham JanardhanCommented:
sorry i miss read ur question i think this is what u want

http://www.w3.org/TR/xml-stylesheet/
0
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.

 
Gautham JanardhanCommented:
public static void Transform(string sXmlPath, string sXslPath){
       
        try{
           
            //load the Xml doc
            XPathDocument myXPathDoc = new XPathDocument(sXmlPath) ;

            XslTransform myXslTrans = new XslTransform() ;
           
            //load the Xsl
            myXslTrans.Load(sXslPath) ;
           
            //create the output stream
            XmlTextWriter myWriter = new XmlTextWriter
                        ("result.html", null);
           
            //do the actual transform of Xml
            myXslTrans.Transform(myXPathDoc,null, myWriter);        

            myWriter.Close() ;


        }catch(Exception e){

            Console.WriteLine("Exception: {0}", e.ToString());
        }
       
    }
0
 
YZlatCommented:
can you give me an example of xml format?
0
 
YZlatCommented:
public void WriteXml(DataSet ds, string filePath )
            {
                  try
                  {
                        m_writer = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);

                        m_writer.WriteStartDocument(true);

                        //<markers>
                        m_writer.WriteStartElement("markers");

                        //write the rest of the records
                        this.WriteRecords(ds.Tables[0]);

                        // </markers>
                         m_writer.WriteEndElement();

                        m_writer.WriteEndDocument();
                  }
                  catch (Exception ex)
                  {
                        Console.Write("ERROR: " + ex.Message);
                  }
                  finally
                  {
                        m_writer.Close();
                  }
            }

            private void WriteRecords(DataTable dt)
            {
                  try
                  {
                        foreach(DataRow dtRow in dt.Rows)
                        {
                              // <nodename>
                              m_writer.WriteStartElement("nodename");
                              m_writer.WriteAttributeString("attribute1", "", dtRow["field1"].ToString());
                                                            // </nodename>
                              m_writer.WriteEndElement();
                        }
                  }
                  catch (Exception ex)
                  {
                        Console.Write("ERROR: " + ex.Message);
                  }
      }
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now