C# 从ASP.NET代码隐藏写入/覆盖特定XML文件
我有一个XML世界地图,它基本上需要两个输入(它是一个包含相应CSS文件的XML文件)国家输入和国家地址。因此,当我手动将数据输入XML文件(国家名称和国家地址)时,地图上的国家会改变其颜色,在该国家上空悬停时,我可以看到我输入的内容 我有一个数据库中所有国家的列表。所以我在想,有什么方法可以让我在所有这些国家将数据库中的数据写入XML文件。我在想这样的事情:C# 从ASP.NET代码隐藏写入/覆盖特定XML文件,c#,css,asp.net,xml,webforms,C#,Css,Asp.net,Xml,Webforms,我有一个XML世界地图,它基本上需要两个输入(它是一个包含相应CSS文件的XML文件)国家输入和国家地址。因此,当我手动将数据输入XML文件(国家名称和国家地址)时,地图上的国家会改变其颜色,在该国家上空悬停时,我可以看到我输入的内容 我有一个数据库中所有国家的列表。所以我在想,有什么方法可以让我在所有这些国家将数据库中的数据写入XML文件。我在想这样的事情: for(int i=0; i<list.count;i++) { list[i].CounryName = //write
for(int i=0; i<list.count;i++)
{
list[i].CounryName = //write it into the XML file;
list[i].CountryUserAddress = //Write it into the XML file;
}
for(int i=0;i以下是一个示例,说明如何使用您提供的数据执行此操作:
public string EditDoc()
{
string filename = "Path/MyFileName.xml";
List<string> list = new List<string>();
if (File.Exists(filename)) //we have the file, so update it
{
XmlDocument myDoc = new XmlDocument(); //create a document object
myDoc.Load(filename); //load existing info
XmlNode root = myDoc.DocumentElement; //the root node ("Country")
XmlNode nodeToUpdate = root.SelectSingleNode("CountryName"); //select the node to update
nodeToUpdate.Value = "new info"; //give it a new value
myDoc.Save(filename); //save the document
}
else //didn't find the file
{
XmlDocument myDoc = new XmlDocument(); //create a new document
XmlNode countryList = myDoc.CreateElement("CountryList");
myDoc.AppendChild(countryList);
for (int i = 0; i < list.Count; i++)
{
XmlNode country = myDoc.CreateElement("Country"); //create the parent "Country" element
myDoc.AppendChild(countryList); //append to the list
XmlNode countryName = myDoc.CreateElement("CountryName"); //create element for "CountryName"
countryName.AppendChild(myDoc.CreateTextNode(list[i].CountryName)); //add data from list index
country.AppendChild(countryName); //append this as a child to "Country"
XmlNode countryUserAddress = myDoc.CreateElement("CountryUserAddress"); //create element for "CountryUserAddress"
countryUserAddress.AppendChild(myDoc.CreateTextNode(list[i].CountryUserAddress)); //add data from list index
country.AppendChild(countryUserAddress); //append as a child to "Country"
}
myDoc.Save(filename); //save the document
}
}
公共字符串EditDoc()
{
string filename=“Path/MyFileName.xml”;
列表=新列表();
如果(File.Exists(filename))//我们有这个文件,那么更新它
{
XmlDocument myDoc=新建XmlDocument();//创建文档对象
加载(文件名);//加载现有信息
XmlNode root=myDoc.DocumentElement;//根节点(“国家”)
XmlNode nodeToUpdate=root.SelectSingleNode(“CountryName”);//选择要更新的节点
nodeToUpdate.Value=“new info”;//给它一个新值
myDoc.Save(文件名);//保存文档
}
else//没有找到该文件
{
XmlDocument myDoc=新建XmlDocument();//创建新文档
XmlNode countryList=myDoc.CreateElement(“countryList”);
myDoc.AppendChild(国家列表);
for(int i=0;i
一般的想法是遍历文档的树并选择要更新的值。可能有更好的方法,但这是我熟悉的方法。同样,您也可以以相同的方式创建xml文档
主题有所不同,但这极大地帮助了我理解XML数据的读写:
编辑:我稍微更新了代码,使父元素为“CountryList”,并将数据库中的每个“Country”附加到该元素上。文档最终将显示如下内容:
<CountryList>
<Country>
<CountryName>Blah</CountryName>
<CountryUserAddress>Blah</CountryUserAddress>
</Country>
<Country>
<CountryName>Blah</CountryName>
<CountryUserAddress>Blah</CountryUserAddress>
</Country>
</CountryList>
废话
废话
废话
废话
以下是一个示例,说明如何使用您提供的数据执行此操作:
public string EditDoc()
{
string filename = "Path/MyFileName.xml";
List<string> list = new List<string>();
if (File.Exists(filename)) //we have the file, so update it
{
XmlDocument myDoc = new XmlDocument(); //create a document object
myDoc.Load(filename); //load existing info
XmlNode root = myDoc.DocumentElement; //the root node ("Country")
XmlNode nodeToUpdate = root.SelectSingleNode("CountryName"); //select the node to update
nodeToUpdate.Value = "new info"; //give it a new value
myDoc.Save(filename); //save the document
}
else //didn't find the file
{
XmlDocument myDoc = new XmlDocument(); //create a new document
XmlNode countryList = myDoc.CreateElement("CountryList");
myDoc.AppendChild(countryList);
for (int i = 0; i < list.Count; i++)
{
XmlNode country = myDoc.CreateElement("Country"); //create the parent "Country" element
myDoc.AppendChild(countryList); //append to the list
XmlNode countryName = myDoc.CreateElement("CountryName"); //create element for "CountryName"
countryName.AppendChild(myDoc.CreateTextNode(list[i].CountryName)); //add data from list index
country.AppendChild(countryName); //append this as a child to "Country"
XmlNode countryUserAddress = myDoc.CreateElement("CountryUserAddress"); //create element for "CountryUserAddress"
countryUserAddress.AppendChild(myDoc.CreateTextNode(list[i].CountryUserAddress)); //add data from list index
country.AppendChild(countryUserAddress); //append as a child to "Country"
}
myDoc.Save(filename); //save the document
}
}
公共字符串EditDoc()
{
string filename=“Path/MyFileName.xml”;
列表=新列表();
如果(File.Exists(filename))//我们有这个文件,那么更新它
{
XmlDocument myDoc=新建XmlDocument();//创建文档对象
加载(文件名);//加载现有信息
XmlNode root=myDoc.DocumentElement;//根节点(“国家”)
XmlNode nodeToUpdate=root.SelectSingleNode(“CountryName”);//选择要更新的节点
nodeToUpdate.Value=“new info”;//给它一个新值
myDoc.Save(文件名);//保存文档
}
else//没有找到该文件
{
XmlDocument myDoc=新建XmlDocument();//创建新文档
XmlNode countryList=myDoc.CreateElement(“countryList”);
myDoc.AppendChild(国家列表);
for(int i=0;i
一般的想法是遍历文档的树并选择要更新的值。可能有更好的方法,但这是我熟悉的方法。同样,您也可以以相同的方式创建xml文档
主题有所不同,但这极大地帮助了我理解XML数据的读写:
编辑:我稍微更新了代码,使父元素为“CountryList”,并将数据库中的每个“Country”附加到该元素上。文档最终将显示如下内容:
<CountryList>
<Country>
<CountryName>Blah</CountryName>
<CountryUserAddress>Blah</CountryUserAddress>
</Country>
<Country>
<CountryName>Blah</CountryName>
<CountryUserAddress>Blah</CountryUserAddress>
</Country>
</CountryList>
废话
废话
废话
废话
我实际上是通过将XML文件的静态部分存储到字符串中来解决的……然后动态添加缺少的部分,这样我就得到了我需要的完整XML文件!我实际上是通过存储XM的静态部分来解决的