C# 如何使用c更新xml元素#

C# 如何使用c更新xml元素#,c#,asp.net,visual-studio-2012,C#,Asp.net,Visual Studio 2012,我有一个带有照片和用户标签的xml文件。 现在我有了我的网站,可以从本地pc浏览图片并上传到web应用程序img文件夹中,成功上传图片后,应该通过刷新网站在网站中显示。但问题是,新浏览图像的xml标记没有生成,因此网站不会在网站中显示上传的图像 下面是隐藏的代码 protected void Page_Load(object sender, EventArgs e) { } protected void btnSubmit_Click(object sender, EventAr

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

下面是隐藏的代码

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);

  }
}
受保护的无效页面加载(对象发送方,事件参数e)
{
}
受保护的void btnsupmit\u单击(对象发送者,事件参数e)
{
//在尝试保存文件之前,请验证
//FileUpload控件包含一个文件。
if(FileUpload1.HasFile)
{
//调用助手方法例程以保存文件。
保存文件(FileUpload1.PostedFile);
XmlDocument doc=新的XmlDocument();
//字符串路径=@“C:\Users\khant\Documents\visual studio 2012\Projects\UploadImageRetrieve\UploadImageRetrieve\photos.xml”;
Load(“C:\\Users\\khant\\Documents\\visual studio 2012\\Project\\UploadImageRetrieve\\UploadImageRetrieve\\photos.xml”);
字符串filenm=FileUpload1.FileName;
XmlElement用户=doc.CreateElement(“用户”);
xmlement photo=doc.CreateElement(“photo”);
XmlText phototext=doc.CreateTextNode(“img/”+filenm);
//doc.SelectSingleNode(“/appSettings/add”).Attributes[“value”].value=“hello”;
//doc.SelectNodes(“/appSettings/add”).Item(2).属性[“value”]。value=“hello”;
照片.儿童(照片文本);
用户.AppendChild(照片);
doc.Save(“C:\\Users\\khant\\Documents\\visual studio 2012\\Project\\UploadImageRetrieve\\UploadImageRetrieve\\photos.xml”);
}
其他的
//通知用户未上载文件。
UploadStatusLabel.Text=“您没有指定要上载的文件。”;
}
无效保存文件(HttpPostedFile文件)
{
//指定将上载文件保存到的路径。
string savePath=“C:\\Users\\khant\\Documents\\visual studio 2012\\Project\\UploadImageRetrieve\\UploadImageRetrieve\\img\\”;
//获取要上载的文件的名称。
字符串文件名=FileUpload1.fileName;
//创建路径和文件名以检查重复项。
字符串路径检查=保存路径+文件名;
//创建用于检查重复项的临时文件名。
字符串tempfileName=“”;
//检查是否已存在具有的文件
//与要上载的文件同名。
if(System.IO.File.Exists(pathToCheck))
{
int计数器=2;
while(System.IO.File.Exists(pathToCheck))
{
//如果具有此名称的文件已存在,
//在文件名前面加一个数字。
tempfileName=计数器.ToString()+文件名;
pathToCheck=保存路径+临时文件名;
计数器++;
}
fileName=tempfileName;
//通知用户文件名已更改。
UploadStatusLabel.Text=“已存在同名文件。”+
“
您的文件已另存为”+文件名; } 其他的 { //通知用户文件已成功保存。 UploadStatusLabel.Text=“您的文件已成功上载。”; } //将要上载的文件名附加到路径。 savePath+=文件名; //调用SaveAs方法保存上传的文件 //文件到指定的目录。 FileUpload1.SaveAs(savePath); } }
请让我知道我错在哪里


谢谢。

您需要在保存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");

谢谢Damith我已经添加了行,但它显示了错误"此文档已经有一个“DocumentElement”节点…我的xml文件代码如下:img/test.jpg img/diers.jpg img/testpng.png,但现在的问题是,通过单击按钮上载的图像存储在web应用程序的img文件夹中,但实际上它没有显示在visual studio的img文件夹中保存在img文件夹中。。。我可以在C:\\Users\\khant\\Documents\\visual studio 2012\\Project\\UploadImageRetrieve\\UploadImageRetrieve\\img\\中看到…我尝试刷新网站,但上传的图像仍不显示…如果再次运行网站,则只有上传图像显示在网站中…因此我需要添加任何回发代码吗?Thanksk thx…如果您知道上传后如何重新绑定列表项的代码…那么请让我知道Thankscal,方法与第一次加载数据相同