xml文档删除所有空节点方法 [英] xml document remove all empty nodes method

查看:321
本文介绍了xml文档删除所有空节点方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好我正在创建一个非常庞大的XmlDocument。 xmlDocument doc = new xmlDocument();.我正在寻找删除所有空节点的方法,在xmlDocument中将其保存到服务器之前。我想在保存之前调用该方法。



Hi guys i am creating an pretty huge XmlDocument. xmlDocument doc = new xmlDocument();. and im looking for for method that removes all empty nodes, in xmlDocument before saving it to the server. i would like to call the method before saving.

XmlDocument doc = new XmlDocument();
     XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
     doc.AppendChild(docNode);

     XmlNode CreateTravelNode = doc.CreateElement("CreateBooking");
     doc.AppendChild(CreateTravelNode);

     XmlNode RecordNode = doc.CreateElement("RecordType");
     RecordNode.AppendChild(doc.CreateTextNode("CreateBooking"));
     CreateTravelNode.AppendChild(RecordNode);

     XmlNode dateFormat = doc.CreateElement("DateFormat");
     dateFormat.AppendChild(doc.CreateTextNode("DDMMYYYY"));
     CreateTravelNode.AppendChild(dateFormat);


     XmlNode prefixNode = doc.CreateElement("WebPrefix");
     prefixNode.AppendChild(doc.CreateTextNode("PD"));
     CreateTravelNode.AppendChild(prefixNode);

     XmlNode PassNode = doc.CreateElement("PassengerCount");
     PassNode.AppendChild(doc.CreateTextNode(antal));
     CreateTravelNode.AppendChild(PassNode);



我的文档还有更多内容,但只是一个例子:)


and there is much more to my document, but just an example :)

推荐答案

我不知道你为什么要删除空节点,而不是先创建这样的节点,但我们假设你真的需要它。请注意,此类操作可能会创建不符合原始 XML方案的XML。无论如何,你甚至没有尝试过。



有什么问题?你有 XmlNode 这个类,方法是 RemoveChild

https://msdn.microsoft.com/en-us/library/system。 xml.xmlnode%28v = vs.110%29.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.removechild%28v=vs。 110%29.aspx [ ^ ]。



属性 XmlNode.ChildNodes 将允许你获得所有孩子,找出是否有(考虑你的空节点的标准)和递归清理你需要的东西:https://msdn.microsoft.com/en-us/library/system.xml.xmlnode .childnodes%28v = vs.110%29.aspx [ ^ ]。



-SA
I don't know why do you want to remove empty nodes, instead of not creating such nodes in first place, but let's assume you really need it. Just note that such operation may create XML which does not fit original XML scheme. Anyway, you did not even try.

What's the problem? You have the class XmlNode with the method RemoveChild:
https://msdn.microsoft.com/en-us/library/system.xml.xmlnode%28v=vs.110%29.aspx[^],
https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.removechild%28v=vs.110%29.aspx[^].

The property XmlNode.ChildNodes will allow you to get all children, find out if there are any (consider your criteria for "empty node") and recursively clean up what you need to: https://msdn.microsoft.com/en-us/library/system.xml.xmlnode.childnodes%28v=vs.110%29.aspx[^].

—SA


这篇关于xml文档删除所有空节点方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆