使用ASP.net和VB.net(或C#)从XML文件编辑和删除数据

时间:2021-07-08 01:41:14

In my web application, i have an XML file called "answers.xml"

在我的Web应用程序中,我有一个名为“answers.xml”的XML文件

it store the user entries in XML

它以XML格式存储用户条目

<?xml version=""1.0""?> <Answers>  
<AnswerSet> <Answer questionId=""MRN"">4444</Answer> 
<Answer questionId=""FName"">test</Answer> 
<Answer questionId=""LName"">patient</Answer> 
<Answer questionId=""AddressPt"">blah blah</Answer> 
<Answer questionId=""Governorate"">xxxx</Answer> 
<Answer questionId=""InitialCSF"">Negative</Answer> 
<Answer questionId=""Diagnosis""></Answer> 
<Answer questionId=""Description""> </Answer>   
</AnswerSet>   
<AnswerSet> 
<Answer questionId=""MRN"">1</Answer> 
<Answer questionId=""FName"">1</Answer> 
<Answer questionId=""LName"">1</Answer> 
<Answer questionId=""AddressPt"">1</Answer> 
<Answer questionId=""InitialCSF"">Positive</Answer> 
<Answer questionId=""Diagnosis"">dx</Answer> 
<Answer questionId=""Description""> </Answer>   
</AnswerSet>  </Answers>

i can add data to the XML file using a DLL file i downloaded from the internet. i need a way to change the data (edit / delete) in the xml file using ASP.net / VB.net or C#

我可以使用从互联网上下载的DLL文件将数据添加到XML文件中。我需要一种方法来改变使用ASP.net / VB.net或C#的xml文件中的数据(编辑/删除)

3 个解决方案

#1


3  

I prefer to use XDocument, because simply you can search it and change the elements or attributes:

我更喜欢使用XDocument,因为只需搜索它并更改元素或属性:

XDocument doc1 = XDocument.Parse("<AnswerSet> <Answer questionId=\"10\" FName=\"test\"> </Answer></AnswerSet> ");
// or if you have related file simply use XDocument doc1 = XDocument.Load(fileFullName);
var element =
      doc1.Descendants("AnswerSet").Elements("Answer")
      .Where(x => x.Attribute("FName") != null 
            && x.Attribute("FName").Value == "test").SingleOrDefault();
if (element != null)
{
   var attr = element.Attribute("FName");
   attr.Value = "Changed";
}

doc1.Save(filePath);

Edit: Descendants("AnswerSet") finds AnswerSet elements, Elements("Answer") finds Answer Elements,

编辑:Descendants(“AnswerSet”)找到AnswerSet元素,Elements(“Answer”)找到Answer Elements,

Where(x => x.Attribute("FName") != null 
            && x.Attribute("FName").Value == "test").SingleOrDefault();

finds element which contains attribute FName and attribute value equals to test, SingleOrDefault in the last, says you should have just one such an element, Also you can change it (just call ToList()) to find all related elements, and finally in if I'll change the value of element, Also at the end we save it again with changed values.

查找包含属性FName和属性值等于test的元素,最后的SingleOrDefault表示你应该只有一个这样的元素,你也可以改变它(只需调用ToList())来查找所有相关的元素,最后在if中我将更改元素的值,最后我们再次使用更改的值保存它。

This language (linq2xml) is too easy and functions like Descendant and Elements are most use full functions in it, so there is no need to have special knowledge you can simply come up to many problems by knowing this functions.

这种语言(linq2xml)太简单了,像Descendant这样的函数和Elements大多数都使用了完整的函数,因此不需要具备特殊知识就可以通过了解这些函数来解决许多问题。

#2


2  

You can just use the XmlDocument class that comes with .Net. No need to download something. Or do I miss something?

您可以使用.Net附带的XmlDocument类。无需下载任何东西。或者我会错过什么?

First thing I found, it's for VB, but the concept stays the same for c#.
http://support.microsoft.com/kb/317662

我发现的第一件事是,它适用于VB,但c#的概念保持不变。 http://support.microsoft.com/kb/317662

You can just load any xml file and then use XPath to access any node and change it.

您可以只加载任何xml文件,然后使用XPath访问任何节点并进行更改。

#3


1  

Have you looked at the XmlDataSource Control.

你看过XmlDataSource控件了吗?

#1


3  

I prefer to use XDocument, because simply you can search it and change the elements or attributes:

我更喜欢使用XDocument,因为只需搜索它并更改元素或属性:

XDocument doc1 = XDocument.Parse("<AnswerSet> <Answer questionId=\"10\" FName=\"test\"> </Answer></AnswerSet> ");
// or if you have related file simply use XDocument doc1 = XDocument.Load(fileFullName);
var element =
      doc1.Descendants("AnswerSet").Elements("Answer")
      .Where(x => x.Attribute("FName") != null 
            && x.Attribute("FName").Value == "test").SingleOrDefault();
if (element != null)
{
   var attr = element.Attribute("FName");
   attr.Value = "Changed";
}

doc1.Save(filePath);

Edit: Descendants("AnswerSet") finds AnswerSet elements, Elements("Answer") finds Answer Elements,

编辑:Descendants(“AnswerSet”)找到AnswerSet元素,Elements(“Answer”)找到Answer Elements,

Where(x => x.Attribute("FName") != null 
            && x.Attribute("FName").Value == "test").SingleOrDefault();

finds element which contains attribute FName and attribute value equals to test, SingleOrDefault in the last, says you should have just one such an element, Also you can change it (just call ToList()) to find all related elements, and finally in if I'll change the value of element, Also at the end we save it again with changed values.

查找包含属性FName和属性值等于test的元素,最后的SingleOrDefault表示你应该只有一个这样的元素,你也可以改变它(只需调用ToList())来查找所有相关的元素,最后在if中我将更改元素的值,最后我们再次使用更改的值保存它。

This language (linq2xml) is too easy and functions like Descendant and Elements are most use full functions in it, so there is no need to have special knowledge you can simply come up to many problems by knowing this functions.

这种语言(linq2xml)太简单了,像Descendant这样的函数和Elements大多数都使用了完整的函数,因此不需要具备特殊知识就可以通过了解这些函数来解决许多问题。

#2


2  

You can just use the XmlDocument class that comes with .Net. No need to download something. Or do I miss something?

您可以使用.Net附带的XmlDocument类。无需下载任何东西。或者我会错过什么?

First thing I found, it's for VB, but the concept stays the same for c#.
http://support.microsoft.com/kb/317662

我发现的第一件事是,它适用于VB,但c#的概念保持不变。 http://support.microsoft.com/kb/317662

You can just load any xml file and then use XPath to access any node and change it.

您可以只加载任何xml文件,然后使用XPath访问任何节点并进行更改。

#3


1  

Have you looked at the XmlDataSource Control.

你看过XmlDataSource控件了吗?