XElement 和 XslCompiledTransform 的问题
我在使用 XElement
和 XslCompiledTransform
的组合时遇到一些问题。我已将我正在使用的示例代码放在下面。如果我使用 GetXmlDocumentXml() 方法获取输入 XML,则它可以正常工作。如果我改用 GetXElementXml()
方法,则在调用 XslComiledTransform
的 Transform
方法时会收到 InvalidOperationException
:
状态 Start 中的令牌文本将导致无效的 XML 文档。如果要编写 XML 片段,请确保将 ConformanceLevel 设置设置为 ConformanceLevel.Fragment 或 ConformanceLevel.Auto。
XElement
和 XmlDocument
上的 CreateNavigator
方法都会返回一个 XPathNavigator
。 XmlDocument
做了什么额外的事情才能使这一切正常工作,我如何对 XElement
做同样的事情?我只是在做一些疯狂的事情吗?
static void Main(string[] args)
{
XslCompiledTransform stylesheet = GetStylesheet(); // not shown for brevity
IXPathNavigable input = this.GetXElementXml();
using (MemoryStream ms = this.TransformXml(input, stylesheet))
{
XmlReader xr = XmlReader.Create(ms);
xr.MoveToContent();
}
}
private MemoryStream TransformXml(
IXPathNavigable xml,
XslCompiledTransform stylesheet)
{
MemoryStream transformed = new MemoryStream();
XmlWriter writer = XmlWriter.Create(transformed);
stylesheet.Transform(xml, null, writer);
transformed.Position = 0;
return transformed;
}
private IXPathNavigable GetXElementXml()
{
var xml = new XElement("x", new XElement("y", "sds"));
return xml.CreateNavigator();
}
private IXPathNavigable GetXmlDocumentXml()
{
var xml = new XmlDocument();
xml.LoadXml("<x><y>sds</y></x>");
return xml.CreateNavigator();
}
I'm having some trouble using a combination of XElement
and XslCompiledTransform
. I've put the sample code I'm using below. If I get my input XML using the GetXmlDocumentXml()
method, it works fine. If I use the GetXElementXml()
method instead, I get an InvalidOperationException
when calling the Transform
method of XslComiledTransform
:
Token Text in state Start would result in an invalid XML document. Make sure that the ConformanceLevel setting is set to ConformanceLevel.Fragment or ConformanceLevel.Auto if you want to write an XML fragment.
The CreateNavigator
method on both XElement
and XmlDocument
returns an XPathNavigator
. What extra stuff is XmlDocument
doing so this all works, and how can I do the same with XElement
? Am I just doing something insane?
static void Main(string[] args)
{
XslCompiledTransform stylesheet = GetStylesheet(); // not shown for brevity
IXPathNavigable input = this.GetXElementXml();
using (MemoryStream ms = this.TransformXml(input, stylesheet))
{
XmlReader xr = XmlReader.Create(ms);
xr.MoveToContent();
}
}
private MemoryStream TransformXml(
IXPathNavigable xml,
XslCompiledTransform stylesheet)
{
MemoryStream transformed = new MemoryStream();
XmlWriter writer = XmlWriter.Create(transformed);
stylesheet.Transform(xml, null, writer);
transformed.Position = 0;
return transformed;
}
private IXPathNavigable GetXElementXml()
{
var xml = new XElement("x", new XElement("y", "sds"));
return xml.CreateNavigator();
}
private IXPathNavigable GetXmlDocumentXml()
{
var xml = new XmlDocument();
xml.LoadXml("<x><y>sds</y></x>");
return xml.CreateNavigator();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
哦,那很容易。解决方案是将
XElement
包装在XDocument
对象中。问题解决了!Oh, that was easy. The solution was to wrap the
XElement
in anXDocument
object. Problem solved!