添加XML节点

添加XML节点

 /// <summary>
    /// 初始化XML status-0
    /// </summary>
    /// <returns></returns>
    private bool InitialConfig()
    {
        bool bRet = false;
        try
        {
            PatchID = Guid.NewGuid().ToString();
            string dirXml = ConfigurationManager.AppSettings["xmlFilePath"];
            XmlDocument doc = new XmlDocument();
            doc.Load(dirXml);
            XmlNode rootNode = doc.DocumentElement;
            //XmlNode dataInfo = rootNode.SelectSingleNode("/DataInfo") as XmlNode;
            XmlElement decompress = doc.CreateElement("Decompress");
            decompress.SetAttribute("PatchID", PatchID);
            XmlElement status = doc.CreateElement("Status");
            status.InnerText = "0";
            XmlElement createDate = doc.CreateElement("CreateDate");
            createDate.InnerText = DateTime.Now.ToString();
            decompress.AppendChild(status);
            decompress.AppendChild(createDate);
            rootNode.AppendChild(decompress);
            doc.Save(dirXml);
            bRet = true;
        }
        catch (Exception ex)
        {
            LogManager.WriteTextLog("WebTest", "Test", "InitialConfig", ex.Message);
        }
        return bRet;
    }

 

<?xml version="1.0" encoding="utf-8"?>
<DataInfo>
  <Decompress PatchID="e7aea488-9e76-49b8-a6e0-ef4f01092c31">
    <Status>1</Status>
    <CreateDate>2010-1-12 18:33:25</CreateDate>
  </Decompress>
  <Decompress PatchID="a44f5a95-8ab6-4519-b71d-d95989dc3101">
    <Status>1</Status>
    <CreateDate>2010-1-12 18:33:37</CreateDate>
  </Decompress>
</DataInfo>

你可能感兴趣的:(添加XML节点)