c# - 如何向 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", new XElement("eventid", "ID-3"),
new XElement("title", "title3"),
new XElement("description", "desc3"),
new XElement("location", new XElement("lat", "lat3"),
new XElement("long", "long3"),
new XElement("address", "addresses3")),
new XElement("datetimestamp", "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")));
推荐阅读
- css - CSS 类不在 iOS 浏览器中显示,但在 Android 中有效
- maven-3 - 如何在命令行中使用 maven 3 设置插件存储库
- python - 从 dbus 对象打印结果
- r - ggplot 抛出错误 `label not found`,而 `label` 显然存在
- jquery - 使用嵌套 JSON 对象自定义(枢轴)显示 Jqgrid
- java - 图片按钮调整大小
- php - 每次用户点击来自 7 的联系人的提交按钮时,如何触发 recaptcha v3(分数测试)
- php - Symfony json 响应返回内容两次
- reactjs - 如何在 React Static typescript 模板中导入 static.config.js 中的 .ts 文件
- java - Maria DB - JPA/Hibernate 更新查询由于错误的查询生成而引发异常