如何追加在C#中的XML文件? [英] how to append a xml file in c#?

查看:133
本文介绍了如何追加在C#中的XML文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我加入跟踪的一个简单的过程中,我已经建立作为一个.exe文件,并在调度运行,每10分钟设置审计。我想有应用程序的输出结果到XML文件中。

如果该文件存在,那么打开,将数据添加到它,如果它不存在,我想创建一个将被保存并在接下来的运行中使用新的XML文件。

这是我的$ C $现在C,我需要什么补充,我怎么打开XML文件(在C:/file.xml)?并用它来添加节点

 静态无效的主要(字串[] args)
{
    XmlDocument的文档=新的XmlDocument();

    XmlDeclaration DEC = doc.CreateXmlDeclaration(1.0,NULL,NULL);
    doc.AppendChild(DEC); //创建根元素

    的XmlElement根= doc.CreateElement(STATS);
    doc.AppendChild(根);

    的XmlElement urlNode = doc.CreateElement(保活);
    urlNode.SetAttribute(运行,DateTime.Now.ToString());

    尝试
    {
        WebProxy WP =新WebProxy(http://proxy.ml.com:8083/);
        Web客户端W =新的Web客户端();
        w.Proxy =可湿性粉剂;

        如果(w.DownloadString(http://wwww.example.com)!=)
            urlNode.SetAttribute(结果,向上);
        其他
            urlNode.SetAttribute(结果,DOWN);
    }
    抓住
    {
        urlNode.SetAttribute(结果,DOWN);
    }
    最后
    {
        root.AppendChild(urlNode);
        doc.Save(C:/keepAlive.xml);
    }
}
 

解决方案

您不能追加一个XML文件 - 你必须加载该文件在内存中,修改/添加/等,然后将其写入到磁盘。

编辑:

那么,对于加载一个文件,你可以使用:

 的XmlDocument xmlDoc中=新的XmlDocument(); //创建一个XML文档对象。
如果(System.IO.File.Exists(yourXMLFile.xml)
    xmlDoc.Load(yourXMLFile.xml); //从文件加载
其他{
   //创建XML文档的结构
   的XmlElement根= xmlDoc.CreateElement(STATS);
   xmlDoc.AppendChild(根);
}
 

,然后开始增加存活的东西。

我真的各地去远一点,而不是乱用XML。我想创建一个包含我需要的一切,只是序列化和反序列化类。

这样的:

  [XmlRoot]
公共类统计{
  公开统计(){}
  公众的IList< StatsItem>项目{获取;集;}
}

公共类StatsItem {
  公共StatsItem(){}

  公共字符串UrlName {获取;集;}
  公开日期时间日期{获取;集;}
}
 

现在只是序列化这一点,你有你的XML文档。届时,反序列化,添加内容到项目列表和序列化,并将其重新保存到磁盘。

有很多关于谷歌的资源,所以只查了一下这些。

i am adding tracing for audit purposes of a simple process i have built as an .exe and set in the scheduler to run every 10 minutes. i want to have the application output the results into an xml file.

if the file exists then open and append data to it, if it does not exist i want to create a new xml file that will be persisted and used on next run.

here is my code now, what do i need to add, how do i open the xml file (on c:/file.xml) and use it to append nodes to?

static void Main(string[] args)
{
    XmlDocument doc = new XmlDocument();

    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
    doc.AppendChild(dec);// Create the root element

    XmlElement root = doc.CreateElement("STATS");
    doc.AppendChild(root);

    XmlElement urlNode = doc.CreateElement("keepalive");
    urlNode.SetAttribute("runTime", DateTime.Now.ToString());

    try
    {
        WebProxy wp = new WebProxy("http://proxy.ml.com:8083/");
        WebClient w = new WebClient();
        w.Proxy = wp;

        if (w.DownloadString("http://wwww.example.com") != "")
            urlNode.SetAttribute("result", "UP");
        else
            urlNode.SetAttribute("result", "DOWN");
    }
    catch
    {
        urlNode.SetAttribute("result", "DOWN");
    }
    finally
    {
        root.AppendChild(urlNode);
        doc.Save("c:/keepAlive.xml");
    }
}

解决方案

You can't append an XML file - you'll have to load the file in memory , modify/add/etc, and then write it to disk.

EDIT :

Well, for loading a file you would use :

XmlDocument xmlDoc= new XmlDocument(); // create an xml document object.
if(System.IO.File.Exists("yourXMLFile.xml")
    xmlDoc.Load("yourXMLFile.xml");// load from file
else{
   // create the structure of your xml document
   XmlElement root = xmlDoc.CreateElement("STATS");
   xmlDoc.AppendChild(root);
}

and then start adding the keepalive stuff.

I would actually go a bit further and not mess around with xml. I'd create a class that contains everything I need and just serialize and deserialize it.

Like this:

[XmlRoot]
public class Stats{
  public Stats(){}
  public IList<StatsItem> Items{get;set;} 
}

public class StatsItem{
  public StatsItem(){}     

  public string UrlName{get;set;}
  public DateTime Date{get;set;}
}

now just serialize this, and you have your xml document. When the time comes, deserialize it, add stuff to the Items list and serialize and save it to disk again.

There are lots of resources on google , so just search a bit for those.

这篇关于如何追加在C#中的XML文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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