如何解决如何将新元素添加到SOAP XML
我有一个SOAP XML文件名“ LogEvents.xml”,我想从中添加/删除元素。
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://www.w3.org/2001/12/soap-envelope" SOAP-ENV:encodingStyle="http://www.w3.org/2001/12/soap-encoding" >
<SOAP-ENV:Body xmlns:lle="http://www.examplee.org/logevents" >
<Event>
<eventid>ID-1</eventid>
<title>title1</title>
<description>desc1</description>
<location>
<lat>1.357207</lat>
<long>103.944880</long>
<address>address1</address>
</location>
<datetimestamp>datetime1</datetimestamp>
</Event>
<Event>
<eventid>ID-2</eventid>
<title>title2</title>
<description>desc2</description>
<location>
<lat>1.304121</lat>
<long>103.831950</long>
<address>addres2</address>
</location>
<datetimestamp>datetime2</datetimestamp>
</Event>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
下面显示了我尝试使用Visual Studio失败的代码。
XDocument xDoc1 = XDocument.Load(@"C:\LogEvents.xml");
xDoc1.Element("SOAP-ENV").Add(new XElement("Event",new XElement("eventid","strEventID"),new XElement("title","title2"),new XElement("description","desc3"),new XElement("location",new XElement("lat","lat3"),new XElement("long","long3"),new XElement("address","addresses3")),new XElement("datetimestamp","DateTime3")));
我试图删除XML文件中的SOAP信封,并用“事件”替换它,并相应地编辑C#代码,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<!--Changed to Events-->
<Events>
<Event>
<eventid>ID-1</eventid>
<title>title1</title>
<description>desc1</description>
<location>
<lat>1.357207</lat>
<long>103.944880</long>
<address>address1Edge</address>
</location>
<datetimestamp>datetime1</datetimestamp>
</Event>
<Event>
<eventid>ID-2</eventid>
<title>title2</title>
<description>desc2</description>
<location>
<lat>1.304121</lat>
<long>103.831950</long>
<address>addres2</address>
</location>
<datetimestamp>datetime2</datetimestamp>
<!--Changed to Events-->
</Event>
<Events>
XDocument xDoc1 = XDocument.Load(@"C:\LogEvents.xml");
// Changed SOAP-ENV to Events
xDoc1.Element("Events").Add(new XElement("Event","ID-3"),"title3"),"DateTime3")));
是否有任何方法可以忽略SOAP信封或解决该信封的方法?
解决方法
使用根。始终可以在需要时从根获取默认名称空间。
XDocument xDoc1 = XDocument.Load(FILENAME);
XElement soap = xDoc1.Root;
XNamespace ns = soap.GetDefaultNamespace();
soap.Add(new XElement("Event",new XElement("eventid","strEventID"),new XElement("title","title2"),new XElement("description","desc3"),new XElement("location",new XElement("lat","lat3"),new XElement("long","long3"),new XElement("address","addresses3")),new XElement("datetimestamp","DateTime3")));
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。