如何将xmlns属性插入XML文件 [英] How to insert xmlns attribute into an XML file

查看:75
本文介绍了如何将xmlns属性插入XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个XML文件,需要在根元素中插入xmlns ="my namespace URL".

另外,我需要在所有元素中插入xmlns =".

我该怎么办?
还可以做到这一点(将xmlns ="添加到所有元素中)而无需遍历每个节点吗?

在此先感谢

问候.

Hi,

I have an XML file and I need to insert xmlns="my namespace url" into the root element.

Also I need to insert xmlns="" into all elements.

How can I do that?
Also it is possible to do this (add xmlns="" into all elements) without looping through each node?

Thanks in advance

Regards.

推荐答案

我不认为这是您正在寻找的准确或最佳的做事方法,但我想我会添加它在这里,以防您找到更好的解决方案:
I don''t think this is what you are looking for exactly or the best way to do things, but I thought I''d add it here in case it leads you to a better solution:
private void TestDocument()
{
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<animal><dog>Doggo</dog><cat>Catsy</cat></animal>");
    AddAttribute(doc.DocumentElement, "xmlns", "");
    StringWriter sw = new StringWriter();
    XmlTextWriter xtw = new XmlTextWriter(sw);
    doc.WriteTo(xtw);
    MessageBox.Show(sw.ToString());
}

private void AddAttribute(XmlNode node, string attribute, string value)
{
    if (node.NodeType == XmlNodeType.Element)
    {
        node.Attributes.Append(node.OwnerDocument.CreateAttribute(attribute));
        node.Attributes[attribute].Value = value;
        foreach (XmlNode subnode in node.ChildNodes)
        {
            AddAttribute(subnode, attribute, value);
        }
    }
}


这篇关于如何将xmlns属性插入XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆