簡體   English   中英

XComment在關閉標簽之前添加

[英]XComment added before closing tag

Visual Studio2008。我正在使用System.Xml.Linq。

我正在編寫XDocument XML文件,並且想在XML元素之前或之后添加注釋行。

出於某種原因,XML序列化器將注釋插入XML元素的中間。

// Create an XComment
//
string roleComment = string.Format("{0} Role ID={1}", myRole.roleType, myRole.roleId);
XComment xRoleComment = new XComment(roleComment);

// Create an XElement
//
XElement xRoleId = new XElement("Role_id", myRole.GUID);

// Add the XComment to the XElement
xRoleId.Add(xRoleComment);

我的輸出意外地以結束標記前的注釋結束:

<Role_id>2510<!--ROLE_TYPE_MASTER Role ID=130--></Role_id>

如何添加注釋,使其最終在元素標簽之外? 之前或之后都可以。

您可以添加注釋節點的 Role_id element.First獲取父然后添加XComment 。或者創建一個父元素Role_id

 XElement parent = new XElement("parent", 
            new XElement("Role_id", myRole.GUID), 
            new XComment(roleComment));

或使用XElement.AddAfterSelf方法:

xRoleId.AddAfterSelf(xRoleComment);

暫無
暫無

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

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