C# XML规范化在转换的输出中返回空元素
我有一个问题,就是如何使用XPath语句从XmlDocument中选择节点 让SelectNodes工作的唯一方法是创建一个非默认名称空间“x”,然后在XPath语句中显式引用节点 虽然这可以工作并为我提供一个节点列表,但是规范化无法为输出中的选定节点生成任何内容 我尝试过使用XmlDsigExcC14NTransform并指定名称空间,但这会产生相同的输出 下面是生成的xml输出示例(使用my中的xml):C# XML规范化在转换的输出中返回空元素,c#,xml,canonicalization,C#,Xml,Canonicalization,我有一个问题,就是如何使用XPath语句从XmlDocument中选择节点 让SelectNodes工作的唯一方法是创建一个非默认名称空间“x”,然后在XPath语句中显式引用节点 虽然这可以工作并为我提供一个节点列表,但是规范化无法为输出中的选定节点生成任何内容 我尝试过使用XmlDsigExcC14NTransform并指定名称空间,但这会产生相同的输出 下面是生成的xml输出示例(使用my中的xml): 另一个StackOverflow用户有 在使用这段新代码时,我发现结果会有所不同,这取
另一个StackOverflow用户有 在使用这段新代码时,我发现结果会有所不同,这取决于将节点传递到LoadInput方法的方式。实现下面的代码是可行的 我仍然很好奇,为什么它会以一种方式工作,而不是另一种方式,但会留下来以防万一
static void Main(string[] args)
{
string path = @"..\..\TestFiles\Test_1.xml";
if (File.Exists(path) == true)
{
XmlDocument xDoc = new XmlDocument();
xDoc.PreserveWhitespace = true;
using (FileStream fs = new FileStream(path, FileMode.Open))
{
xDoc.Load(fs);
}
//Instantiate an XmlNamespaceManager object.
System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xDoc.NameTable);
//Add the namespaces used to the XmlNamespaceManager.
xmlnsManager.AddNamespace("x", "http://www.myApps.co.uk/");
// Create a list of nodes to have the Canonical treatment
XmlNodeList nodeList = xDoc.SelectNodes("/x:ApplicationsBatch/x:Applications|/x:ApplicationsBatch/x:Applications//*", xmlnsManager);
//Initialise the stream to read the node list
MemoryStream nodeStream = new MemoryStream();
XmlWriter xw = XmlWriter.Create(nodeStream);
nodeList[0].WriteTo(xw);
xw.Flush();
nodeStream.Position = 0;
// Perform the C14N transform on the nodes in the stream
XmlDsigC14NTransform transform = new XmlDsigC14NTransform();
transform.LoadInput(nodeStream);
// use a new memory stream for output of the transformed xml
// this could be done numerous ways if you don't wish to use a memory stream
MemoryStream outputStream = (MemoryStream)transform.GetOutput(typeof(Stream));
File.WriteAllBytes(@"..\..\TestFiles\CleanTest_1.xml", outputStream.ToArray());
}
}
嗨,这是v.v.v.旧的,但我想你没有用来生成哈希和加密的代码吧?我正在解决同样的问题。。。。。。。。。谢谢
static void Main(string[] args)
{
string path = @"..\..\TestFiles\Test_1.xml";
if (File.Exists(path) == true)
{
XmlDocument xDoc = new XmlDocument();
xDoc.PreserveWhitespace = true;
using (FileStream fs = new FileStream(path, FileMode.Open))
{
xDoc.Load(fs);
}
//Instantiate an XmlNamespaceManager object.
System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(xDoc.NameTable);
//Add the namespaces used to the XmlNamespaceManager.
xmlnsManager.AddNamespace("x", "http://www.myApps.co.uk/");
// Create a list of nodes to have the Canonical treatment
XmlNodeList nodeList = xDoc.SelectNodes("/x:ApplicationsBatch/x:Applications|/x:ApplicationsBatch/x:Applications//*", xmlnsManager);
//Initialise the stream to read the node list
MemoryStream nodeStream = new MemoryStream();
XmlWriter xw = XmlWriter.Create(nodeStream);
nodeList[0].WriteTo(xw);
xw.Flush();
nodeStream.Position = 0;
// Perform the C14N transform on the nodes in the stream
XmlDsigC14NTransform transform = new XmlDsigC14NTransform();
transform.LoadInput(nodeStream);
// use a new memory stream for output of the transformed xml
// this could be done numerous ways if you don't wish to use a memory stream
MemoryStream outputStream = (MemoryStream)transform.GetOutput(typeof(Stream));
File.WriteAllBytes(@"..\..\TestFiles\CleanTest_1.xml", outputStream.ToArray());
}
}