首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用XElement、XAttribute添加XML命名空间值

如何使用XElement、XAttribute添加XML命名空间值
EN

Stack Overflow用户
提问于 2015-02-13 16:47:15
回答 1查看 332关注 0票数 0

以下是所需的输出:

代码语言:javascript
复制
<BatchAttestationRequest xmlns="http://cms.gov/nlr/attestation/batch/request/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://cms.gov/nlr/attestation/batch/request/1.0 BatchAttestationRequest.xsd ">

我找到了一些有用的帖子让我朝着正确的方向前进。This question让我非常接近,但是当在我身上实现时,解决方案会出现一些问题。

这是我的代码:

代码语言:javascript
复制
        XNamespace ns = @"http://cms.gov/nlr/attestation/batch/request/1.0";
        XNamespace xsi = @"http://www.w3.org/2001/XMLSchema-instance";


        XElement xml_file = new XElement(ns + "BatchAttestationRequest",
                                new XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName),
                                new XAttribute(xsi + "schemaLocation", @"http://cms.gov/nlr/attestation/batch/request/1.0 BatchAttestationRequest.xsd "),
                                    header, // child nodes
                                    attestations,
                                    trailer);

我在这个代码中遇到的问题是:

  • 属性是无序的;"xmlns“属性需要放在第一位。
  • 我的根节点的每个子节点现在都有“xmlns=”作为属性。

我想得到上面的输出。

谢谢!

EN

回答 1

Stack Overflow用户

发布于 2015-02-13 20:13:51

您需要为元素设置名称空间,就像为根设置名称空间一样。例如新的XElement(ns + "Header")

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28504548

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档