开发者

Converting xml document structure from one to another in c#

开发者 https://www.devze.com 2022-12-20 21:58 出处:网络
Currently I have an xml structure in an app.I needed to convert it from one structure to another.I have the xsds for it.The app is in C#.Naturally I thou开发者_JAVA百科ght of using good old coding to

Currently I have an xml structure in an app. I needed to convert it from one structure to another. I have the xsds for it. The app is in C#. Naturally I thou开发者_JAVA百科ght of using good old coding to convert it, but that sounded like the least efficient idea. Someone recommended me to use XSLT, but I'm not 100% sure how it works.

Does anyone know how to use XSLT to convert an xml structure to another? Examples would be Nice. Or are there any other free applications that would do a better job?

Thanks.


You can use the XslCompiledTransform class to invoke XSLT. The documentation has lots of examples.


Have a look at: XslCompiledTransform Class


        using (XmlWriter myWriter =  XmlWriter.Create("result.html"))
        {
            string xmlPath = ""; // xml to transform
            string xslPath = ""; // xsl path

            XPathDocument myXPathDoc = new XPathDocument(xmlPath);
            XslCompiledTransform xslTrans = new XslCompiledTransform();

            //load the Xsl 
            xslTrans.Load(xslPath);

            //do the actual transform of Xml
            xslTrans.Transform(myXPathDoc, null, myWriter);
        }


  1. This is what XSLT is for. The XML-to-HTML examples you're finding are really a special case. A very useful special case, but even so.
  2. XSLT is not insanely difficult to learn, if you have a good book. Michael Kay's XSLT Programmer's Reference used to be definitive, but I haven't needed a book on XSLT for years so I don't know if it still is.


Support your friendly book author. I read about this topic this week in the book Pro LINQ. XSLT is something I never considered using, but I think I might be needed it pretty soon for XML conversions.

0

精彩评论

暂无评论...
验证码 换一张
取 消