在 C++、XPath、Windows 窗体中按属性对 XmlDocument 中的节点进行排序
我想按子节点的属性对 XML 文档进行排序。我正在 Visual C++ 中使用 Windows 窗体。我尝试使用Stackoverflow上提出的解决方案 。但不知怎的,它行不通。
我的未排序 XML-Doc 如下所示:
<Message-List>
<Message sendTime="0"></Message>
<Message sendTime="20"></Message>
<Message sendTime="5"></Message>
</Message-List>
我的排序 XML-Doc 应该如下所示:
<Message-List>
<Message sendTime="0"></Message>
<Message sendTime="5"></Message>
<Message sendTime="20"></Message>
</Message-List>
我尝试遵循代码,但 checkme-返回的字符串是0205。所以排序根本不起作用。
System::Xml::XmlDocument^ sourceXmlDoc = gcnew XmlDocument;
sourceXmlDoc->LoadXml("<Message-List><Message sendTime=\"0\"></Message><Message sendTime=\"20\"></Message><Message sendTime=\"5\"></Message></Message-List>");
System::Xml::XPath::XPathNavigator^ navigator = sourceXmlDoc->CreateNavigator();
System::Xml::XPath::XPathExpression^ selectExpression = navigator->Compile("Message-List/Message");
System::Xml::XPath::XPathExpression^ sortExpr = navigator->Compile("@sendTime");
selectExpression->AddSort(sortExpr, XmlSortOrder::Ascending, XmlCaseOrder::None, "", XmlDataType::Text);
System::Xml::XPath::XPathNodeIterator^ nodeIterator = navigator->Select(selectExpression);
String^ checkMe;
while (nodeIterator->MoveNext())
{
if (nodeIterator->Current->MoveToFirstAttribute())
{
checkMe = checkMe + nodeIterator->Current->Value;
}
}
此外,我对如何在 while 循环中继续进行感到困惑。如何将重新排序的 xmlDoc 保存为 XmlDocument?
I want to sort a XML-Document by attributes of an Child-Node. I am working with Windows Forms in Visual C++. I tried to use the solution proposed here on Stackoverflow. But somehow it won't work.
My unsorted XML-Doc looks like the following:
<Message-List>
<Message sendTime="0"></Message>
<Message sendTime="20"></Message>
<Message sendTime="5"></Message>
</Message-List>
My sorted XML-Doc should look like this:
<Message-List>
<Message sendTime="0"></Message>
<Message sendTime="5"></Message>
<Message sendTime="20"></Message>
</Message-List>
I tried following Code, but the checkme-String returned is 0205. So the sorting doesn't even work.
System::Xml::XmlDocument^ sourceXmlDoc = gcnew XmlDocument;
sourceXmlDoc->LoadXml("<Message-List><Message sendTime=\"0\"></Message><Message sendTime=\"20\"></Message><Message sendTime=\"5\"></Message></Message-List>");
System::Xml::XPath::XPathNavigator^ navigator = sourceXmlDoc->CreateNavigator();
System::Xml::XPath::XPathExpression^ selectExpression = navigator->Compile("Message-List/Message");
System::Xml::XPath::XPathExpression^ sortExpr = navigator->Compile("@sendTime");
selectExpression->AddSort(sortExpr, XmlSortOrder::Ascending, XmlCaseOrder::None, "", XmlDataType::Text);
System::Xml::XPath::XPathNodeIterator^ nodeIterator = navigator->Select(selectExpression);
String^ checkMe;
while (nodeIterator->MoveNext())
{
if (nodeIterator->Current->MoveToFirstAttribute())
{
checkMe = checkMe + nodeIterator->Current->Value;
}
}
Furthermore, I am stuck on how to proceed in the while-loop. How can I save the resorted xmlDoc as XmlDocument?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能完全错了,因为 .NET 不是我的事,但是你的 navigator->Compile 中的 xpath 不是错误的,selectExpression->AddSort 中的 XML 数据类型是错误的吗
I might be completely wrong since .NET is not my thing, but isn't your xpath in navigator->Compile wrong and your XML data type in selectExpression->AddSort wrong
最后,我成功地对 XML 消息进行了排序。
@John:是的,你是对的,我错过了更改XmlDataType。谢谢你的提示。
以下解决方案创建 XmlDocument 的副本并按数字属性“sendTime”对其进行排序。
Finally, I managed to sort the XML-Message.
@John: yes you were right, I missed to change the XmlDataType. Thanks for the hint.
The following Solution makes a Copy of a XmlDocument and sorts it by the numerical Attribute "sendTime".
我刚刚看到这个解决方案,但在 .NET 4.0 中你可以使用 linq 代替。我不会将其移植到 C++,但你会明白的!它是一个静态函数,按属性排序,调用此方法可将旧节点替换为排序后的节点。
对于通话,我使用这个:
I just saw this solution, but in .NET 4.0 you could use linq instead. I won't port it to C++, but you'll get the idea! It is a static function that sorts by attribute and the call to this method that replaces the old node with the sorted node.
And for the call, I use this: