簡體   English   中英

通過代碼C#在xml中的特定位置添加注釋

[英]Add comment to specific position in xml by code C#

我知道我可以使用XElement生成XML,

XElement xmlTree1 = new XElement("Root",
    new XElement("Child3", 3),
    new XElement("Child4", 4),
    new XElement("Child5", 5),
);

Console.WriteLine(xmlTree2);

結果是

<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
  <Child5>5</Child5>
</Root>

我的問題是,是否有一些方法可以通過代碼使用C#將符號添加到xml中的特定位置?

我想做的就是

XElement xmlTree1 = new XElement("Root",
    new XElement("Child3", 3),
    new XElement("Child4", 4),

    //Maybe Some Way to Add Comment Like This
    new COMMENT(String.Format("This is a comment test!!!!")),  

    new XElement("Child5", 5),
);

Console.WriteLine(xmlTree2);

結果是

<Root>
  <Child3>3</Child3>
  <Child4>4</Child4>
   <!-- This is a comment test!!!! -->      
  <Child5>5</Child5>
</Root>

謝謝

我認為答案就像使用XComment一樣簡單:

XElement xmlTree1 = new XElement("Root",
    new XElement("Child3", 3),
    new XElement("Child4", 4),

    //Maybe Some Way to Add Comment Like This
    new XComment(String.Format("This is a comment test!!!!")),  

    new XElement("Child5", 5),
);

暫無
暫無

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

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