如何在不加载整个xml文档的情况下更新xml值的元素? [英] How to Update an element of xml value without loading the whole xml document?

查看:117
本文介绍了如何在不加载整个xml文档的情况下更新xml值的元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨Codians,
我几天前正面临一个问题;
我的情况是,
我想从xml文件中为特定值加载特定元素,然后更改该值并将其写入xml中的已获取元素索引位置.
我想知道这怎么可能?我已经在asp.net论坛和stackoverflow中发布了主题,但我仍在寻找该问题的解决方案.

Hi Codians,
I am facing an issue from couple of days ago;
My scenario is as,
I want to load the particular element from the xml file for a particular value and then to change the value and to write it in the xml file in the place fetched element index.
I am wondering how it will be possible? I have posted threads in the asp.net forum and stackoverflow as well but still i am in the search of the solution for this issue.

<gpx>
    <waypoints>
      <waypoint a="3">
        <name>name of waypoint</name>
        <description> dscrtiption</description>
     </waypoint>
     <waypoint a="2">
       <name>second waypoint</name>
       <description/>
     </waypoint>
   </waypoints>
</gpx>


我正在尝试的是仅加载其a ="2"的航路点,并将其名称值从第二航路点"更改为某物",并将该航路点a ="2"替换为其名称值将为新的航路点xml文件中的内容"放入磁盘.
谢谢


What I am trying is to load only the waypoint whose a="2", and to change its name value from "second waypoint" to "something" and to replace the waypoint a="2" to new waypoint whose name value will be "something" in the xml file onto disk.
Thanks

推荐答案

这是它的工作方式:

Here is how it will work :

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"C:\Well.xml");
XmlNode node = xmlDoc.SelectSingleNode("/gpx/waypoints/waypoint[@a='2']/name");
node.InnerText = "Something";
xmlDoc.Save(@"C:\Well.xml");



希望能有所帮助! :)

更新1:
从问题的主题来看,您似乎不想加载整个XML.
好吧,有一个类XmlTextWriter,它是基于流的XML加载,它不会加载整个XML.与基于内存的XML加载XmlDocument 相反,它将逐个节点读取,它将整个XML加载到内存中.

现在回到重点,在互联网上搜索更多信息后,就像我想的那样,XmlTextWriter 无法修改Xml节点.因此,您的解决方案将是使用XmlDocument itself来修改节点.

但是,我确实以这种方式尝试过:



Hope it helped! :)

Update 1:
From the subject of your question, it seems that you don''t want to load the whole XML.
Well there is a class XmlTextWriter which is a stream-based loading of XML and it won''t load the whole XML. It will read node by node, in contrast to XmlDocument which is a in-memory based loading of XML, it loads the whole XML into memory.

Now coming back to the point, after searching for more info on the internet, it was just like I thought, XmlTextWriter cannot modify the Xml nodes. So your solution will be to use the XmlDocument itself for Modifying the nodes.

I did however tried in this way :

XmlTextReader reader = new XmlTextReader(path);
while (reader.Read())
{
    if (reader.NodeType == XmlNodeType.Element && reader.Name == "waypoint")
    {
        if (reader.GetAttribute("a") == "2")
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(reader.ReadOuterXml());
            XmlNode node = xmlDoc.SelectSingleNode(".");
            //String str = node.InnerText;
            node.InnerText = "Something";
            //The above statement gave this error : Object reference not set to an instance of an object.
        }
    }
}


:(

所以它对我没有用.
我仍将尝试找出解决方案.

更新2:

在CodeProject中找到了一些文章.根据您的要求,检查以下内容:

-首先,您可以将大型xml文件拆分为较小的文件.检查本文: http://www.codeproject.com/KB/XML/SplitLargeXMLintoSmallFil.aspx#xx2994884xx [ ^ ]

-找到节点后,将较小的XML加载到XmlDocument中,然后像在解决方案中一样进行编辑.然后像本文中提到的那样合并xml文件: http://www.codeproject.com/KB/XML/XStreamingElement_Merge .aspx [^ ]

试试看.祝你好运!


:(

So it didn''t work for me.
I will still try to figure out a solution for this.

Update 2:

Found some articles in CodeProject. According your requirements, check these :

-First you can split your large xml file into smaller ones. Check this article : http://www.codeproject.com/KB/XML/SplitLargeXMLintoSmallFil.aspx#xx2994884xx[^]

- After finding the node, load the smaller XML into an XmlDocument and then edit like I did in my solution. Then merge the xml files like mentioned in this article : http://www.codeproject.com/KB/XML/XStreamingElement_Merge.aspx[^]

Try it out. Good luck!


嗨 穆罕默德·侯赛因,

请执行以下步骤:

Hi Muhammadhussain,

Please do the following steps:

XmlDocument doc = new XmlDocument();
doc.LoadXml("Path.xml");
XmlElement root = doc.DocumentElement;

for (int n = 0; n < doc.DocumentElement.ChildNodes[0].ChildNodes.Count; n++)
{
    XmlNodeList waypoint = doc.DocumentElement.ChildNodes[0].ChildNodes[n];
    for(int w = 0; w < waypoint.Count; w++)
    {
        string waypointAttribute = waypoint.Attributes["a"].Value;
        if(waypointAttribute == "2")
        {
            waypoint.Attributes["name"] = "somthing";
        }
    }
}
doc.Save("Path.xml");



谢谢,
Imdadhusen



Thanks,
Imdadhusen


您要尝试的操作有点不切实际,但是如果您愿意,可以采用一种将其视为平面文件的方法.与其将其视为要解析的XML文档,不如将其作为平面文件打开,读取数据块并进行启发式猜测,以了解要跳过要修改的区域之前需要跳过或预读多少个字节.一旦找到数据,就需要对其进行更改,修改并将其写回到文件中并退出.尝试查找数据,同时还要确保确保预读次数最少,这将是一项相当大的工作,但这是一个可行的解决方案.
What you are trying to do is kinda unrealistic, but if there''s a way to do it as you wish to, treat the file as a flat file. Instead of treating it as an XML document to parse, open the file as a flat file, read chunks of data, and make heuristic guesses as to how many bytes you need to skip or read ahead to hit the area you want to modify. And once you hit the data you need to change, modify it, and write it back to the file, and exit. It''ll be a fair bit of work trying to locate the data while also trying to make sure you have minimal number of read-aheads but it''s a workable solution.


这篇关于如何在不加载整个xml文档的情况下更新xml值的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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