有条件地修改肥皂响应

时间:2018-12-18 05:45:47

标签: c# xml soap xml-parsing response

我想根据作为参数传递的Country修改/删除soap响应xml中的节点,在此xml中,如果Country =” US”,我想删除节点。

我尝试使用以下代码,但未选择节点。

XDocument doc = XDocument.Parse(“XMLString”));

XNamespace xmlns = " http://schemas.xmlsoap.org/soap/envelope/";

var addressNode = doc.Descendants(xmlns + " AddressData").Elements(xmlns + " Country");

任何人都可以建议根据国家/地区删除节点的方法。

样本Xml:

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <GetAddressDataRs xmlns="xyz.com/Services/Schemas/ Address /v1" xmlns:cm="xyz.com/Services/Schemas/Common/v1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="xyz.com/Services/Schemas/Address /Schemas.xsd">
         <AddressData>
            <Id xsi:type="xsd:string">1</Id>
            <Add1>ABCDEFG</Add1>
            <Add2>XYZABV</Add2>
            <City>Las Vegas</City>
            <Country>US</Country>
         </AddressData>
         <AddressData>
            <Id xsi:type="xsd:string">1</Id>
            <Add1>ABCDEFG</Add1>
            <Add2>XYZABV</Add2>
            <City>Phoenix</City>
            <Country>US</Country>
         </AddressData>
         <AddressData>
            <Id xsi:type="xsd:string">1</Id>
            <Add1>ABCDEFG</Add1>
            <Add2>XYZABV</Add2>
            <City>Kansas City</City>
            <Country>US</Country>
         </AddressData>
         <AddressData>
            <Id xsi:type="xsd:string">1</Id>
            <Add1>ABCDEFG</Add1>
            <Add2>XYZABV</Add2>
            <City>Sydney</City>
            <Country>Australia</Country>
         </AddressData>
         <AddressData>
            <Id xsi:type="xsd:string">1</Id>
            <Add1>ABCDEFG</Add1>
            <Add2>XYZABV</Add2>
            <City>Townsville</City>
            <Country>Australia</Country>
         </AddressData>
      </GetAddressDataRs>
   </s:Body>
</s:Envelope>

1 个答案:

答案 0 :(得分:0)

尝试按照xml linq进行操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Linq ;

namespace ConsoleApplication91
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            var usa = doc.Descendants().Where(x => x.Name.LocalName == "AddressData").Where(x => x.Elements().Any(y => (y.Name.LocalName == "Country") && ((string)y == "US"))).FirstOrDefault();

        }
    }
}