如何添加使用的XmlDocument在C#.NET CF 3.5属性的XML [英] How to add attributes to xml using XmlDocument in c# .net CF 3.5

查看:123
本文介绍了如何添加使用的XmlDocument在C#.NET CF 3.5属性的XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个属性ABC与preFIXXX为元素AAA。下面code增加了preFIX但同时也增加了的namespaceURI的元素。

I need to create an attribute "abc" with the prefix "xx" for an element "aaa". The following code adds the prefix but it also adds the namespaceUri to the element.

所需的输出:

<mybody>
<aaa xx:abc="ddd"/>
<mybody/>

我的code:

My Code:

  XmlNode node = doc.SelectSingleNode("//mybody");
  XmlElement ele = doc.CreateElement("aaa");

  XmlAttribute newAttribute = doc.CreateAttribute("xx","abc",namespace);              
  newAttribute.Value = "ddd";

  ele.Attributes.Append(newAttribute);

  node.InsertBefore(ele, node.LastChild);

以上code产生:

The above code generates :

<mybody>
<aaa xx:abc="ddd" xmlns:xx="http://www.w3.org/1999/XSL/Transform"/>
<mybody/>

所需的输出为

Desired output is

<mybody>
<aaa xx:abc="ddd"/>
<mybody/>

和所述的xx属性的声明应该在像根节点来完成:

And the declaration of the "xx" attribute should be done in the root node like :

<ns:somexml xx:xsi="http://www.w3.org/1999/XSL/Transform"  xmlns:ns="http://x.y.z.com/Protocol/v1.0">

如何,如果得到的deisred格式输出?如果XML不在此所需的格式,然后它不能再加工..

How can if get the output in the deisred format? If the xml is not in this desired format then it cannot be processed anymore..

谁能帮助?

谢谢, 玉萍

推荐答案

我相信这只是直接在根节点上设置相关属性的问题。下面是一个示例程序:

I believe it's just a matter of setting the relevant attribute directly on the root node. Here's a sample program:

using System;
using System.Globalization;
using System.Xml;

class Test
{
    static void Main()
    {
        XmlDocument doc = new XmlDocument();
        XmlElement root = doc.CreateElement("root");

        string ns = "http://sample/namespace";
        XmlAttribute nsAttribute = doc.CreateAttribute("xmlns", "xx",
            "http://www.w3.org/2000/xmlns/");
        nsAttribute.Value = ns;
        root.Attributes.Append(nsAttribute);

        doc.AppendChild(root);
        XmlElement child = doc.CreateElement("child");
        root.AppendChild(child);
        XmlAttribute newAttribute = doc.CreateAttribute("xx","abc", ns);
        newAttribute.Value = "ddd";        
        child.Attributes.Append(newAttribute);

        doc.Save(Console.Out);
    }
}

输出:

<?xml version="1.0" encoding="ibm850"?>
<root xmlns:xx="http://sample/namespace">
  <child xx:abc="ddd" />
</root>

这篇关于如何添加使用的XmlDocument在C#.NET CF 3.5属性的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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