簡體   English   中英

用帶有XmlWriter命名空間的XmlWriter編寫XElement嗎?

[英]Write XElement with XmlWriter with the XmlWriter namespace?

我有一個XmlWriter,它已經具有設置了xmlns屬性的根元素。

我現在有一個XElement(帶有很多子元素),沒有指定名稱空間。

如果我只是這樣做:

    xElement.WriteTo(writer);

我以無效的xml結尾:

<MyRootElement xmlns="SomeNameSpace">
    <!-- Some elements of this namespace -->

    <MyXElementType  xmlns="">
        <!-- A lot of other Element in the same namespace -->
    </MyXelementType>
    <!-- Some elements of this namespace -->
</MyRootElement>

由於禁止指定空名稱空間。 這是有問題的,因為我需要在XSD之后進行驗證。

我可以做些什么來結束這個:

    <MyXElementType><!-- HERE, no xmlns !!! -->
        <!-- A lot of other Element in the same namespace -->
    </MyXelementType>
    <!-- Some elements of this namespace -->
</MyRootElement>

是什么讓您認為它無效? 規格說明:

默認名稱空間聲明中的屬性值可以為空。 在聲明的范圍內,這具有相同的效果,即沒有默認名稱空間。

如果要刪除它,則需要在編寫XElement之前將其XElement及其后代的名稱空間設置為與現有根元素相同的名稱空間。 創建后,您可以執行以下操作:

XNamespace ns = "SomeNamespace";
var element = new XElement(ns + "MyXElementType");

或之后的事實:

foreach (var element in element.DescendantsAndSelf())
{
    element.Name = ns + element.Name.LocalName;
}

暫無
暫無

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

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