[英]How to add an attribute to XML
I am returning an object from a web service. 我从Web服务返回一个对象。 It arrives in XML format - 它以XML格式到达-
<DailyTracker xmlns="http://schemas.datacontract.org/2004/07/MSI.Web.MSINet.BusinessEntities">
<ClientId>2147483647</ClientId>
<ClientRosterId>2147483647</ClientRosterId>
<Dept>
<DepartmentID>2147483647</DepartmentID>
<DepartmentName>String content</DepartmentName>
<EmailAddress>String content</EmailAddress>
<Location>2147483647</Location>
<PayCode>String content</PayCode>
</Dept>
etc, etc...
</DailyTracker>
This is coming from an asp.net website using c#. 这来自使用c#的asp.net网站。 I am returning an object of type DailyTracker. 我返回的是DailyTracker类型的对象。 how can I add an attribute to one of the elements? 如何为其中一个元素添加属性? Is that possible? 那可能吗? Thanks! 谢谢!
Instantiate an XDocument
using the XML returned from the service. 使用从服务返回的XML实例化XDocument
。 Get the XElement
that you want, then add a new XAttribute
to it: 获取所需的XElement
,然后向其添加新的XAttribute
:
XDocument document = new XDocument(xmlString);
XElement element = document.Element("myElement");
element.Add(new XAttribute("MyAttr", "My Value"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.