使用C#更新XML元素 [英] Updating XML element using C#

查看:124
本文介绍了使用C#更新XML元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

再见,
现在我正在使用XDocument类:

Hi again,
now I''m using the XDocument class:

        XDocument doc2 = XDocument.Parse(@"
    <kml xmlns='http://www.opengis.net/kml/2.2' xmlns:gx='http://www.google.com/kml/ext/2.2' xmlns:kml='http://www.opengis.net/kml/2.2' xmlns:atom='http://www.w3.org/2005/Atom'>
        <Placemark>
             <name>Test</name>
             <LookAt>
            <longitude>11111111111</longitude>
            <latitude>1111111111111</latitude>
            <altitude>500</altitude>
            <range>500</range>
            <tilt></tilt>
            <heading>11111111</heading>
            <altitudeMode>relativeToGround</altitudeMode>
           </LookAt>
          <styleUrl>#msn_ylw-pushpin70</styleUrl>
          <Point>
            <coordinates>111111111,1111111111</coordinates>
          </Point>
      </Placemark>
</kml>");





因此,正如我之前提到的,我需要更新经度"元素的内容,以便使用;





So as I mentioned before that I need to update the "longitude" element content so I used;

doc2.Element("kml").Element("longitude").Value = "XXXXX";



我现在得到异常对象引用未设置为对象的实例"
我知道到达经度"元素存在问题,那么使我能够执行此操作的方法是什么,因为我尝试了很多次并进行了大量搜索..

非常感谢.



I get now the exception "Object reference not set to an instance of an object"
I know that there is a problem reaching the "longitude" element so what is the method which enables me to do it, because I tried many times and searched alot..

Many thanks.

推荐答案

我们不做您的作业:它的设置是有原因的.在这里,您可以考虑自己被告知的内容,并尝试理解它.也可以在那里帮助您的导师识别您的弱点,并将更多的注意力放在补救措施上.

自己尝试,您可能会发现它并不像您想的那么难!

那里有很多例子,只需要您将它们调整到适合您的特定需求即可.询问我需要完整的代码,从加载文件开始,到保存和关闭文件为止."不是在尝试做失败,而是在尝试让我们为您做,因为您不会被打扰,从长远来看,这对任何人都没有帮助.
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

There are a lot of examples out there, that just need you to tweek them to your specific needs. Asking for "I need the full code starting from loading the file and finishing with saving and closing it." is not trying to do it and failing, it''s purely trying to get us to do it for you because you can''t be bothered, and that helps nobody in the long term.



在Internet上可以找到许多不错的文章,描述了如何读取/遍历/更新xml.
您可以使用LINQ to XML机制.

http://msdn.microsoft.com/en-us/library/bb387098.aspx

LINQ to XML [
Hi
You can find many good articles in internet describing how to read/traverse/update xml.
You can use LINQ to XML mechanism.

http://msdn.microsoft.com/en-us/library/bb387098.aspx

LINQ to XML[^]

Or you can simply
1. Read the xml file in to dataset
2. Modify the dataset
3. Update back in to xml.
For Ex:

    DataSet oDs = new DataSet();
oDs.ReadXml("D:\\yourfile.xml");
if (oDs != null && oDs.Tables.Count > 0 && oDs.Tables[0].Rows.Count > 0  && oDs.Tables[1].Rows.Count > 0)
{
    for (int i = 0; i < oDs.Tables[0].Rows.Count; i++)
    {
        oDs.Tables[0].Rows[i]["name"] = "newName";
        oDs.Tables[0].Rows[i]["PlaceMark_ID"] = "newID";
    }
    for (int i = 0; i < oDs.Tables[1].Rows.Count; i++)
    {
        oDs.Tables[0].Rows[i]["lnngtitude"] = "newval";
    }
}
oDs.AcceptChanges();
oDs.WriteXml("D:\\yourfile.xml");



这可能不是最佳解决方案.尝试了解各种机制.并且对此有个好主意.您只要尝试一下就可以了.并且,如果您发现任何错误,则除了期望完整的源代码之外,只需在此处发布即可.最好的



This may not be the best solution. Try to understand various mechanisms. And have a good idea about this. You just try what you know. And if you found any errors, just post here other than expecting the full source code. All the best


我完全同意OriginalGriff.我想说,如果您发布所遇到的问题,那就更好了.

您可以看一下以下链接,它提供了更新xml文档的多种方法之一.

LINQ to XML更新元素值 [ ^ ]
I completely agree with OriginalGriff. I would say it would have been better if you post the problem you faced.

You can take a look at the following link, it provides one of the many ways to update an xml document.

LINQ to XML Update Element Values[^]


这篇关于使用C#更新XML元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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