根据属性值复制 XML 文件的部分内容
我有一个很大的 xml 文件,需要创建该文件的部分“副本”。 (使用C#) 我需要保持相同的 xml 结构,但只需要条件为“true”的部分。
示例结构:
<?xml version="1.0" encoding="utf-8"?>
<country name="Netherlands">
<region name="NH">
<city name="Aalsmeer">
<district name="Some district">
<part type="slum" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="false" />
<residents number="10000" />
<homes number = "7000" />
</city>
<city name="Heemskerk">
<district name="Some district">
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
<region name="ZH">
<city name="Rotterdam">
<district name="Some district">
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
<city name="Moerdijk">
<district name="Some district">
<part type="residential area" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="false" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
</country>
我只需要属性“valid”为“true”的“city”元素。 新的 XML 文件应如下所示:
<?xml version="1.0" encoding="utf-8"?>
<country name="Netherlands">
<region name="NH">
<city name="Heemskerk">
<district name="Some district"
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
<region name="ZH">
<city name="Rotterdam">
<district name="Some district"
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
</country>
如何尽快完成此操作(考虑到城市元素的数量(例如 100630)和文件大小(例如 63,0 MB)?
I've a large xml file and need to create a partial 'copy' of this file. (Using C#)
I need to keep the same xml structure, but only need the parts where a condition is 'true'.
An example structure:
<?xml version="1.0" encoding="utf-8"?>
<country name="Netherlands">
<region name="NH">
<city name="Aalsmeer">
<district name="Some district">
<part type="slum" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="false" />
<residents number="10000" />
<homes number = "7000" />
</city>
<city name="Heemskerk">
<district name="Some district">
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
<region name="ZH">
<city name="Rotterdam">
<district name="Some district">
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
<city name="Moerdijk">
<district name="Some district">
<part type="residential area" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="false" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
</country>
I only need the 'city' elements where the attribute 'valid' is 'true'.
The new XML file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<country name="Netherlands">
<region name="NH">
<city name="Heemskerk">
<district name="Some district"
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
<region name="ZH">
<city name="Rotterdam">
<district name="Some district"
<part type="workersDistrict" />
</district>
<contact adres="StreetName" telephone="0000000000" valid="true" />
<residents number="10000" />
<homes number = "7000" />
</city>
</region>
</country>
How do I get this done as quickly as possible (taking into account the number of city elements (e.g. 100630) and the file size (e.g. 63,0 MB)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用修改后的身份转换,您可以简单地匹配要抑制的元素并提供一个空模板。
您可以像这样在 C# 中执行 XSLT:
Using a modified identity transform, you can simply match on the elements that you want to suppress and provide an empty template.
You can execute the XSLT in C# like this:
我建议您查看 linq to xml:
示例: http://jesseliberty.com /2011/02/15/linq-to-xml/
MSDN:http://msdn.microsoft.com/en-us/library/bb387098.aspx
I suggest you check out linq to xml:
Example: http://jesseliberty.com/2011/02/15/linq-to-xml/
MSDN: http://msdn.microsoft.com/en-us/library/bb387098.aspx