停止 XDocument url 编码字符串
我正在尝试创建一个 XML 文件以符合某人的 XSD,该 XSD 允许在其描述标记中包含 XHTML 内容。我已经缩短了方法层次结构,但基本上它执行以下操作:
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;
}
“Course_Overview”、“Entry_Requirements”等字段包含没有 URL 编码的 XHTML,但当添加到 XDocument 时,它似乎会自动对它们进行编码,因此我最终得到 & ;lt;P>
而不是
。有没有办法阻止这个,或者我必须使用一些东西来代替 XDocument。
我开始使用 XDocument 的原因是代码的布局可能与最终结果类似。
I am trying to create an XML file to conform to someones XSD this XSD allows for XHTML content within its description tag. I have cut short the method hierarchy but basically it does the following:
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;
}
The fields like "Course_Overview", "Entry_Requirements" contain XHTML without URL encoding but when added to the XDocument it seems to automatically encode them so I end up with <P>
instead of <P>
. Is there anyway to stop this or am I going to have to use something instead of XDocument.
The reason I started with XDocument is the layout of the code can appear similar to the end result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加文本内容将被视为您想要文本内容,而不是“内部 XML”。
我认为最好将 XML 字符串转换为文档片段,然后添加该片段(本质上需要将字符串转换为节点,然后添加节点)。
使用
XElement.Parse
创建文档片段。Adding text content is treated as if you wanted text content, and not "inner XML".
I think you would be best converting the string of XML into a Document Fragment, and then adding that fragment (essentially the sring needs to be converted into nodes, and then add the nodes).
Use
XElement.Parse
to create the document fragment.这种行为是正确的。
您想要的是实际解析 HTML 描述并将其插入。看一下
XElement.Parse ()
。我相信以下 MSDN 社交问题很好地解决了您的情况。
This behavior is correct.
What you want is to actually parse the description HTML and insert that. Have a look at
XElement.Parse()
.I believe that the following MSDN Social question addresses your case pretty well.