2010-03-04 78 views
0

我想创建一个XML文件以符合某人的XSD这个XSD允许其描述标签中的XHTML内容。我已剪短方法层次,但基本上它执行以下操作:如“Course_Overview”,“Entry_Requirements”包含XHTML不URL编码,但是当加入的XDocument它似乎自动对其进行编码,所以我停止XDocument网址编码字符串

using System.Xml.Linq; 

public static XDocument Create() 
    {    
     XDocument doc = new XDocument(
      new XDeclaration("1.0", Encoding.UTF8.HeaderName, String.Empty), 
      GetCourses() 
      ); 

     //ValidatingProcess(@".\xcri_cap_1_1.xsd", doc.ToString()); 

     return doc; 
    } 

private static XElement GetCourses(XElement provider) 
    { 
     //datatbale dt generated here from sql 
     for (int i = 0; i < dt.Rows.Count; i++) 
     { 
      provider.Add(new XElement("course", 
        new XElement("identifier", dt.Rows[i]["QN_ID"]), 
        new XElement("title", dt.Rows[i]["LSC Descriptor"]), 
        new XElement("subject", dt.Rows[i]["category"]), 
        new XElement("description", dt.Rows[i]["Course_Overview"], 
        new XAttribute("Type", "Course Overview")), 
        //new XElement("description", dt.Rows[i]["Entry_Requirements"]), 
        //new XAttribute("Type", "Entry Requirements"), 
        new XElement("description", dt.Rows[i]["Course_Contents"], 
        new XAttribute("Type", "Course Contents")), 
        new XElement("description", dt.Rows[i]["Assessment"], 
        new XAttribute("Type", "Assessment")), 
        new XElement("description", dt.Rows[i]["Progression_Route"], 
        new XAttribute("Type", "Progression Route")), 
        new XElement("url", "http://www.nnc.ac.uk/CourseLeaflets/Leaflet2.aspx?qnid=" + dt.Rows[i]["QN_ID"]), 
        GetQualification(dt.Rows[i])//, 
        //GetPresentation(row) 
        )); 
     } 

     return provider; 
    } 

字段最终与&lt;P&gt;而不是<P>。无论如何阻止这个或我将不得不使用的东西,而不是XDocument。

我从XDocument开始的原因是代码的布局可能会出现类似于最终结果。

回答

3

添加文本内容被视为您想要文本内容,而不是“内部XML”。

我认为你最好将XML字符串转换成文档片段,然后添加该片段(实质上需要将sring转换为节点,然后添加节点)。使用XElement.Parse创建文档片段。

+0

谢谢我认为这是推理,但不知道周围的方式。虽然我刚刚发现一些锂标签缺少他们的结束标签,所以我必须通过一些东西来确认它是xhtml。 – PeteT 2010-03-04 14:30:31