如何使用c#更新xml元素

时间:2023-02-01 07:16:44

I have xml file with photo and user tag. Now I have my website with browse the image from local pc and upload into web application img folder, after successfully uploading image, it should be show in the website by refreshing website. But the issue is that the xml tag for new browse image is not generating so website is not show the uploaded images in the website.

我有带照片和用户标签的xml文件。现在我有我的网站浏览本地电脑上的图像并上传到网页应用程序img文件夹,成功上传图片后,应该通过刷新网站在网站上显示。但问题是新浏览图像的xml标签没有生成,因此网站不会显示网站上传的图像。

Below is the code behind.

下面是代码背后的代码。

protected void Page_Load(object sender, EventArgs e)
  {

  }

  protected void btnSubmit_Click(object sender, EventArgs e)
  {
      // Before attempting to save the file, verify
      // that the FileUpload control contains a file.
      if (FileUpload1.HasFile)
      {
          // Call a helper method routine to save the file.
          SaveFile(FileUpload1.PostedFile);


          XmlDocument doc = new XmlDocument();
          //string path = @"C:\Users\khant\Documents\visual studio 2012\Projects\UploadImageRetrieve\UploadImageRetrieve\photos.xml";
          doc.Load("C:\\Users\\khant\\Documents\\visual studio 2012\\Projects\\UploadImageRetrieve\\UploadImageRetrieve\\photos.xml");
          string filenm = FileUpload1.FileName;

          XmlElement user = doc.CreateElement("user");
          XmlElement photo = doc.CreateElement("photo");
          XmlText phototext = doc.CreateTextNode("img/"+filenm);
          //doc.SelectSingleNode("/appSettings/add").Attributes["value"].Value = "hello";
          //doc.SelectNodes("/appSettings/add").Item(2).Attributes["value"].Value = "hello";

          photo.AppendChild(phototext);
          user.AppendChild(photo);
          doc.Save("C:\\Users\\khant\\Documents\\visual studio 2012\\Projects\\UploadImageRetrieve\\UploadImageRetrieve\\photos.xml");
      }
      else
          // Notify the user that a file was not uploaded.
          UploadStatusLabel.Text = "You did not specify a file to upload.";

  }

  void SaveFile(HttpPostedFile file)
  {
      // Specify the path to save the uploaded file to.
      string savePath = "C:\\Users\\khant\\Documents\\visual studio 2012\\Projects\\UploadImageRetrieve\\UploadImageRetrieve\\img\\";

      // Get the name of the file to upload.
      string fileName = FileUpload1.FileName;

      // Create the path and file name to check for duplicates.
      string pathToCheck = savePath + fileName;

      // Create a temporary file name to use for checking duplicates.
      string tempfileName = "";

      // Check to see if a file already exists with the
      // same name as the file to upload.        
      if (System.IO.File.Exists(pathToCheck))
      {
          int counter = 2;
          while (System.IO.File.Exists(pathToCheck))
          {
              // if a file with this name already exists,
              // prefix the filename with a number.
              tempfileName = counter.ToString() + fileName;
              pathToCheck = savePath + tempfileName;
              counter++;
          }

          fileName = tempfileName;

          // Notify the user that the file name was changed.
          UploadStatusLabel.Text = "A file with the same name already exists." +
              "<br />Your file was saved as " + fileName;
      }
      else
      {
          // Notify the user that the file was saved successfully.
          UploadStatusLabel.Text = "Your file was uploaded successfully.";
      }

      // Append the name of the file to upload to the path.
      savePath += fileName;

      // Call the SaveAs method to save the uploaded
      // file to the specified directory.
      FileUpload1.SaveAs(savePath);

  }
}

Please let me know where i am wrong.

请让我知道我错在哪里。

Thanks.

谢谢。

1 个解决方案

#1


1  

You need to add user element to your xml document before saving it

您需要在保存之前将用户元素添加到xml文档中

photo.AppendChild(phototext);
user.AppendChild(photo);
doc.DocumentElement.AppendChild(user); // this line is required 
doc.Save("C:\\Users\\khant\\Documents\\visual studio 2012\\Projects\\UploadImageRetrieve\\UploadImageRetrieve\\photos.xml");

#1


1  

You need to add user element to your xml document before saving it

您需要在保存之前将用户元素添加到xml文档中

photo.AppendChild(phototext);
user.AppendChild(photo);
doc.DocumentElement.AppendChild(user); // this line is required 
doc.Save("C:\\Users\\khant\\Documents\\visual studio 2012\\Projects\\UploadImageRetrieve\\UploadImageRetrieve\\photos.xml");