[英]Add Nested XML Node to xml File using Asp.net and C#
I Want add some url to xml file on my website using C#. 我想使用C#在我的网站上添加一些url到xml文件。
I have already create a XML file on website Root. 我已经在网站Root上创建了一个XML文件。 The content of xml file is :
xml文件的内容是:
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.structure.com/Structure.aspx?id=1</loc>
</url>
</urlset>
Now I want add new <url>
Node with <loc>
node to xml file and I want xml content changes like 现在我想将带有
<loc>
节点的新<url>
节点添加到xml文件中,我想要更改xml内容
<?xml version="1.0" encoding="utf-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<url>
<loc>http://www.structure.com/Structure.aspx?id=1</loc>
</url>
<url>
<loc>http://www.structure.com/Structure.aspx?id=2</loc>
</url>
</urlset>
I try made a function that get url string from web form and it's triggers on asp:Button click 我尝试创建一个函数,从Web表单获取url字符串,并在asp:Button点击触发器
protected void Button1_Click(object sender, EventArgs e)
{
insertSiteMap("http://www.structure.com/Structure.aspx?id=2");
}
And the function is: 功能是:
private void insertSiteMap(string pageurl)
{
//Load XML Schema
System.Xml.XmlDocument originalXml = new System.Xml.XmlDocument();
originalXml.Load(Server.MapPath("../sitemap.xml"));
XmlElement URL = originalXml.CreateElement("url");
XmlElement LOC = originalXml.CreateElement("loc");
XmlText LOCText = originalXml.CreateTextNode(pageurl);
LOC.AppendChild(LOCText);
URL.AppendChild(LOC);
XmlNode newUrl = originalXml.GetElementsByTagName("url")[0];
originalXml.DocumentElement.PrependChild(newUrl);
originalXml.Save(Server.MapPath("../sitemap.xml"));
}
I don't have any error and visual studio message me xml file has been modified but when I open the file there is no any changes on xml file :(. 我没有任何错误和visual studio消息我修改了xml文件,但是当我打开文件时,xml文件没有任何变化:(。
am I did wrong any where? 我在哪里做错了?
Once you have created the new element with your variable URL
, you need to insert with eg originalXml.DocumentElement.AppendChild(URL);
使用变量
URL
创建新元素后,需要插入例如originalXml.DocumentElement.AppendChild(URL);
. 。
However, take note that in your original XML you use a namespace while your C# code creates the new XmlElements in no namespace, so you need to fix the element creation as well, eg 但是,请注意,在原始XML中,您使用命名空间,而C#代码在无命名空间中创建新的XmlElements,因此您还需要修复元素创建,例如
XmlElement url = originalXml.CreateElement("url", originalXml.DocumentElement.NamespaceURI);
XmlElement loc = originalXml.CreateElement("loc", originalXml.DocumentElement.NamespaceURI);
loc.InnerText = pageurl;
url.AppendChild(loc);
originalXml.DocumentElement.AppendChild(url);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.