xml方法有什么区别(XML转换)
有人可以帮我吗?
我需要使用 XSLT 将一个 XML 文档转换为另一个 XML 文档。
所以我有下一个简单的代码:
var xmlDocument = new XmlDocument();
xmlDocument.Load("input.xml");
var xslTransform = new XslCompiledTransform();
var styleSheetFullPath = "DefaultStyleSheet.xsl";
xslTransform.Load(styleSheetFullPath);
输入 xml 文档如下例所示:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Object>
<GUID>201110180954525010129</GUID>
<Meta name="FILENAME" format="string" frate="" />
</Object>
</Root>
我需要将其转换为下一个 XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Object>
<GUID>201110180954525010129</GUID>
<FILENAME/>
</Object>
</Root>
当我尝试使用下一个方法时,它效果很好。但我需要首先将文档写入文件,然后在转换后我需要重新编辑它。
var fileName = "result.xml";
using (var myWriter = new XmlTextWriter(fileName, null))
{
xslTransform.Transform(xmlDocument.CreateNavigator(), null, myWriter);
}
var doc = new XmlDocument();
doc.Load(fileName);
但是,当我尝试使用下一种方法动态创建 XML 文档时
var xmlDocOutput = new XmlDocument();
var xmlDocOutputDeclaration = xmlDocOutput.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocOutput.AppendChild(xmlDocOutputDeclaration);
using (var xmlWriter = xmlDocOutput.CreateNavigator().AppendChild())
{
xslTransform.Transform(xmlDocument.CreateNavigator(), null, xmlWriter);
}
,我得到下一个输出
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Object>
<GUID>201110180954525010129</GUID>
<FILENAME> </FILENAME>
</Object>
</Root>
那么我该怎么做才能保留元素内容中的空白。
提前致谢
can anyone help me please?
I need to transform one XML document to another by using XSLT.
So I have the next simple code:
var xmlDocument = new XmlDocument();
xmlDocument.Load("input.xml");
var xslTransform = new XslCompiledTransform();
var styleSheetFullPath = "DefaultStyleSheet.xsl";
xslTransform.Load(styleSheetFullPath);
Input xml document looks like the example below:
<?xml version="1.0" encoding="utf-8"?>
<Root>
<Object>
<GUID>201110180954525010129</GUID>
<Meta name="FILENAME" format="string" frate="" />
</Object>
</Root>
I need to transform it to next XML:
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Object>
<GUID>201110180954525010129</GUID>
<FILENAME/>
</Object>
</Root>
When I trying to use the next approach it works well. But I need first write document to file and after the transformation I need to reed it.
var fileName = "result.xml";
using (var myWriter = new XmlTextWriter(fileName, null))
{
xslTransform.Transform(xmlDocument.CreateNavigator(), null, myWriter);
}
var doc = new XmlDocument();
doc.Load(fileName);
But when I trying create XML document dynamically by using the next approach
var xmlDocOutput = new XmlDocument();
var xmlDocOutputDeclaration = xmlDocOutput.CreateXmlDeclaration("1.0", "utf-8", null);
xmlDocOutput.AppendChild(xmlDocOutputDeclaration);
using (var xmlWriter = xmlDocOutput.CreateNavigator().AppendChild())
{
xslTransform.Transform(xmlDocument.CreateNavigator(), null, xmlWriter);
}
I get the next output
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<Object>
<GUID>201110180954525010129</GUID>
<FILENAME> </FILENAME>
</Object>
</Root>
So what can I do to preserve white space in element content.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论