2012-01-03 34 views
0

我已经生成一个HTML文件和顶部的html声明如下所示:XML和xmln声明HTML文件不能转化

<html xml:lang="de-CH" lang="de-CH" xmlns="http://www.w3.org/1999/xhtml"> 

然后我尝试将其转换成与此不同的格式。 Net 4代码:

XmlReaderSettings settings = new XmlReaderSettings(); 
settings.DtdProcessing = DtdProcessing.Ignore; 

XslCompiledTransform proc = new XslCompiledTransform(); 

proc.Load("Html_to_Sql.xslt"); 

fsHtmlXml = new FileStream(file.Name, FileMode.Create); 

html = XmlReader.Create(file.FullName, settings); 
proc.Transform(html, null, fsHtmlXml); 

不幸的是,只要我在HTML中具有xml,lang和xmlns属性,就没有任何反应。 这是为什么?

+1

你应该张贴您的XSLT;这个问题最可能在那里。 – Jacob 2012-01-03 22:06:51

回答

1

将通过XML和XPath这项工作

using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Xsl; 
using System.Xml.XPath; 

public class TransformXML 
{ 
    //This will transform xml document using xslt and produce result xml document 
    //and display it 

    public static void Main(string[] args) 
    { 
     try 
     { 
      XPathDocument myXPathDocument = new XPathDocument(sourceDoc); 
      XslTransform myXslTransform = new XslTransform(); 
      XmlTextWriter writer = new XmlTextWriter(resultDoc, null); 
      myXslTransform.Load(xsltDoc); 
      myXslTransform.Transform(myXPathDocument, null, writer); 
      writer.Close(); 
      StreamReader stream = new StreamReader (resultDoc); 
      Console.Write("**This is result document**\n\n"); 
      Console.Write(stream.ReadToEnd()); 
     } 
     catch (Exception e) 
     { 
      Console.WriteLine ("Exception: {0}", e.ToString()); 
     } 
    } 
} 
2

您的XSLT将需要引用http://www.w3.org/1999/xhtml命名空间中的元素。您尚未发布XSLT代码,问题很可能在于该文件。

1

xmlns属性指定XML文档的名称空间。这与C#中的名称空间非常相似,其中两个具有相同名称但名称空间不同的类被认为是完全不同的类。更改XML名称空间意味着您的XSLT模板/ XPath不匹配。