簡體   English   中英

如何使用XElement,XAttribute添加XML命名空間值

[英]How to add XML namespace values using XElement, XAttribute

這是所需的(必需的)輸出:

 <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 "> 

我發現了一些有用的帖子讓我朝着正確的方向前進。 這個問題讓我非常接近,但是在我這方面實施時,解決方案存在一些問題。

這是我的代碼:

        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 =”“”作為屬性。

我想准確得到上面的輸出。

謝謝!

您需要像為root用戶一樣為元素設置命名空間。 例如new XElement(ns +“Header”)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM