[C#] XML-添加新的命名空间 [英] [C#] XML - add a new Namespace

查看:77
本文介绍了[C#] XML-添加新的命名空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有这个,

Hi,

I have this,

<OrderPush xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  ">
    <OrderPush>
        <TaskersIntent/>
        <Task>
            <GroundTask>
                <TaskeeWho

>
...




我想要这个:




I want this :

<OrderPush xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 

 xmlns:bml="http://netlab.gmu.edu/IBML">
    <OrderPush>
        <bml:TaskersIntent/>
        <bml:Task>
            <bml:GroundTask>
                <bml:TaskeeWho

>



该怎么做?
亲切的问候,



How to do this ?
Kind regards,

推荐答案

嗯,您可以通过不同的方式来实现;
Hmm you can do it in different ways;
XmlDocument.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); XmlDocument.DocumentElement.SetAttribute("xmlns:bml", "http://netlab.gmu.edu/IBML");





XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance";  string s = new XElement("OrderPush",     new XAttribute(XNamespace.Xmlns + "xsi", ns),     new XAttribute(ns + "noNamespaceSchemaLocation", "namespace.xsd") ).ToString();



使用XmlWriter:



with XmlWriter:

const string ns = "http://www.w3.org/2001/XMLSchema-instance"; writer.WriteStartDocument(); writer.WriteStartElement("OrderPush"); writer.WriteAttributeString("xmlns", "xsi", "", ns); writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation",       ns, "namespcae.xsd"); writer.WriteEndDocument();


您使用的是旧版XML内容还是Linq-To-Xml?

旧版:

Are you using the legacy XML stuff, or Linq-To-Xml?

Legacy:

XmlElement root = doc.CreateElement("OrderPush"); 
root.SetAttribute("xmlns:xsi", "http://blah blah"); 
root.SetAttribute("xmlns:bml", "http://blah blah blah"); 



Xml-To-Link



Xml-To-Link

XElement root = new XElement("OrderPush", 
                             new XAttribute(XNamespace.Xmlns + "xsi", "http://blah blah"),
                             new XAttribute(XNamespace.Xmlns + "bml", "http://blah blah blah"), 
                             ...); 



或类似的东西.



Or something like that.


这篇关于[C#] XML-添加新的命名空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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