C# XmlDocument loping(foreach)reomving NameSpace“;xmlns";

C# XmlDocument loping(foreach)reomving NameSpace“;xmlns";,c#,xml,asp.net-core,C#,Xml,Asp.net Core,嘿,伙计们,我有一个XML文件,我正在使用XDocument对其进行写入,并使用下一个void删除名称空间 string path = Server.MapPath(xmlpath); XDocument doc = XDocument.Load(path) XElement root = new XElement("url"); foreach (var node in doc.Root.Descendants() .Where(n =&g

嘿,伙计们,我有一个XML文件,我正在使用XDocument对其进行写入,并使用下一个void删除名称空间

string path = Server.MapPath(xmlpath);
XDocument doc = XDocument.Load(path)
XElement root = new XElement("url");
foreach (var node in doc.Root.Descendants()
                           .Where(n => n.Name.NamespaceName == ""))
                {
                    node.Attributes("xmlns").Remove();
                    node.Name = node.Parent.Name.Namespace + node.Name.LocalName;
                }
此函数100%适用于我现在更改的XDocument

XDocument oldDoc = XDocument.Load(path);// the old doucument

XmlDocument newDoc = new XmlDocument();//the new document 
我需要一个函数,允许我从我的节点删除名称空间xmlns,就像上面一样,非常感谢您的时间团队和阅读我的问题

您非常接近,只需要使用
XMLDocument.CreateElement(字符串名称)
重载,返回
XMLNode
,然后使用
.InnerText
属性设置值

输出


伊姆古尔
视频标题
视频解码器
可视API
期间
2050-11-05T19:20:30+08:00
注视
日期
对
不
VideoKindName

删除名称空间后,您能否共享示例xml和结果?有关如何使用XmlDocument的更多详细信息,请参阅:让我们继续讨论如何删除中的xmlns=”“和中的xmlns=“”,我做到了这一点urlRoot.AppendChild(doc.CreateElement(“loc”)。InnerText=”“http:/domain/site.com但我如何才能将“xmlns”从
var mainRoot = doc.DocumentElement; //urlset element
var urlRoot = doc.CreateElement("url"); //create url element
var VidooTree = urlRoot.AppendChild(doc.CreateElement(a, ""));
urlRoot.AppendChild(doc.CreateElement("loc", "http:/domain/site.com"));
VidooTree.AppendChild(doc.CreateElement(b)).InnerText="imgur";
VidooTree.AppendChild(doc.CreateElement(c)).InnerText= "videoTitle";
VidooTree.AppendChild(doc.CreateElement("video:description")).InnerText= "videoDec";
VidooTree.AppendChild(doc.CreateElement(d)).InnerText= "VideoApi";
VidooTree.AppendChild(doc.CreateElement(m)).InnerText= "duration";
VidooTree.AppendChild(doc.CreateElement(nn)).InnerText= "2050-11-05T19:20:30+08:00";
VidooTree.AppendChild(doc.CreateElement(e)).InnerText= "watched";
VidooTree.AppendChild(doc.CreateElement(k)).InnerText= "date";
VidooTree.AppendChild(doc.CreateElement(f)).InnerText= "yes";
VidooTree.AppendChild(doc.CreateElement(g)).InnerText="No";
VidooTree.AppendChild(doc.CreateElement(h)).InnerText= "VideoKindName";
urlRoot.AppendChild(VidooTree);
mainRoot.AppendChild(urlRoot);
<url xmlns="">
 <loc xmlns="http:/domain/site.com" />
  <video>
    <thumbnail_loc>imgur</thumbnail_loc>
    <title>videoTitle</title>
    <description>videoDec</description>
    <content_loc>VideoApi</content_loc>
    <duration>duration</duration>
    <expiration_date>2050-11-05T19:20:30+08:00</expiration_date>
    <view_count>watched</view_count>
    <publication_date>date</publication_date>
    <family_friendly>yes</family_friendly>
    <live>No</live>
    <category>VideoKindName</category>
  </video>
</url>