【发布时间】:2021-01-09 07:26:29
【问题描述】:
我有一个soap响应,我想删除所有与soap相关的东西,并获得只有一个标题的内部XML。
回复:
"<s:Envelope xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
<s:Body>
<getResponse xmlns = ""http://tempuri.org/"">
<getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
<Id>999</Id>
<EResponse>
<au>
<st>ABC</st>
<co>DGH</co>
<mo>MMM</mo>
</au>
</EResponse>
</getResult>
</getResponse>
</s:Body>
</s:Envelope>
我想提取这个:
<getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
<Id>999</Id>
<EResponse>
<au>
<st>ABC</st>
<co>DGH</co>
<mo>MMM</mo>
</au>
</EResponse>
</getResult>
我正在尝试这个:
XDocument input;
using (var nodeReader = new XmlNodeReader(doc))
{
nodeReader.MoveToContent();
input = XDocument.Load(nodeReader);
}
var xns = XNamespace.Get("http://tempuri.org/");
var SoapBody = input.Descendants(xns + "getResult").First().ToString();
但我也在 getresult 标记中附加了命名空间 tempuri,这是我不想要的或任何其他方式来实现它?
【问题讨论】:
-
您在结果中获得命名空间的原因是因为在上层元素中,命名空间被定义为 xmlns="tempuri.org" 这意味着该元素的子元素和属性将有这个特定的命名空间。如果您确实需要没有命名空间的内容,则需要添加一个额外的步骤以从结果中删除所有命名空间。
-
m 也有同样的想法,唯一的方法是删除命名空间,实际上我的最终目标是将它反序列化到我的命名不同的类中,比如 GetResultTrigger。有什么见解吗?
-
要删除 xml 文档中的命名空间,您可以按照此处标记的答案进行操作:stackoverflow.com/questions/987135/… 使用类并不能帮助您删除命名空间。