如何从Api流中删除不需要的节点

时间:2019-03-28 19:55:28

标签: c# xml api

我有C#代码,可产生soap API并将其推送给供应商。这在一定程度上有效,但是当我运行它时,我会不断将"<?xml version="1.0"?>"添加到顶部。可以将其删除,因为另一端的供应商无法解析该部分,从而导致代码出错。如果我在代码中取出xml并手动推送而没有上面的标签,它就可以正常工作。

        HttpWebRequest request = CreateWebRequest();
                    XmlDocument soapEnvelopeXml = new XmlDocument();

                    soapEnvelopeXml.LoadXml($@"
                    <soap:Envelope
                    xmlns:soap=""http://www.w3.org/2003/05/soap-envelope""
                    xmlns:DDAPI=""https://xxxxx.xxx.xxxx/xxx/xxxx/xxxxx"">
                    <DDAPI:GTXML VERSION='1.0' TIME_STAMP='2002-11-22T13:10:21 GMT' TRANSACTION_ID='2002-11-22T13:10:21 GMT.1501'>
                    <DDAPI:HEADER>
                        <DDAPI:SENDER>
                            <DDAPI:SENDER_NAME>xxxxxxx</DDAPI:SENDER_NAME>
                            <DDAPI:SITE_NAME>xxxxxx</DDAPI:SITE_NAME>
                            <DDAPI:SITE_TYPE>xxxxx</DDAPI:SITE_TYPE>
                            <DDAPI:USER_NAME>xxxxxx</DDAPI:USER_NAME>
                            <DDAPI:PASSWORD>xxxxxxxxxxxxx</DDAPI:PASSWORD>
                        </DDAPI:SENDER>
                        <DDAPI:RECEIVER>
                            <DDAPI:RECEIVER_NAME>xxxxxxx</DDAPI:RECEIVER_NAME>
                        </DDAPI:RECEIVER>
                    </DDAPI:HEADER>
                    <DDAPI:BODY>
                        <DDAPI:REQUEST TYPE='import'>
                            <DDAPI:IMPORT_REQUEST TYPE='force-user-profile' VERBOSE='on'>
                                <DDAPI:USER_PROFILE DOC_VERSION='1.9' EVENT_TYPE='upu_event' TIME_STAMP='2002-11-22T14:06:39 GMT'>
                                    <DDAPI:UNIQUE_ID>
                                        <DDAPI:RESOURCE_ID RESOURCE_SITE_NAME='xxxxxxxx'
                                               RESOURCE_SITE_TYPE='corp'
                                               RESOURCE_SUBSITE_NAME='xxxxxxx'>xxxxxxxxx
                                        </DDAPI:RESOURCE_ID>
                                        <DDAPI:RESOURCE_PWD>xxxxxxxxx</DDAPI:RESOURCE_PWD>
                                    </DDAPI:UNIQUE_ID>
                                    <DDAPI:USER_INFO>
                                        <DDAPI:FIRST_NAME>xxxxxxxx</DDAPI:FIRST_NAME>
                                        <DDAPI:LAST_NAME>xxxxx</DDAPI:LAST_NAME>
                                        <DDAPI:EMAIL>xxxxxxx@xxxxxxx.xxx</DDAPI:EMAIL>
                                        <DDAPI:PHONE TYPE='work'>1231231234</DDAPI:PHONE>
                                    </DDAPI:USER_INFO>
                                    <DDAPI:CUSTOM_FIELDS>
                                        <DDAPI:CUSTOM_FIELD NUM='1'>
                                            <DDAPI:CFE_NAME>xxxxxxx</DDAPI:CFE_NAME>
                                            <DDAPI:CFE_VALUE.1>32f3214d345gdt</DDAPI:CFE_VALUE.1>
                                        </DDAPI:CUSTOM_FIELD>
                                        <DDAPI:CUSTOM_FIELD NUM='2'>
                                            <DDAPI:CFE_NAME>xxxxxxx</DDAPI:CFE_NAME>
                                            <DDAPI:CFE_VALUE.1>213f2341gf235g</DDAPI:CFE_VALUE.1>
                                        </DDAPI:CUSTOM_FIELD>
                                        <DDAPI:CUSTOM_FIELD NUM='3'>
                                            <DDAPI:CFE_NAME>xxxxxxxxx</DDAPI:CFE_NAME>
                                            <DDAPI:CFE_VALUE.1>4gb2345bgvf234</DDAPI:CFE_VALUE.1>
                                        </DDAPI:CUSTOM_FIELD>
                                    </DDAPI:CUSTOM_FIELDS>
                                </DDAPI:USER_PROFILE>
                            </DDAPI:IMPORT_REQUEST>
                        </DDAPI:REQUEST>
                    </DDAPI:BODY>
                </DDAPI:GTXML>
            </soap:Envelope>
                                    ");

                    using (Stream stream = request.GetRequestStream())
                    {
            //soapEnvelopeXml.Load(stream);
            //Console.Out.Write(soapEnvelopeXml.DocumentElement.OuterXml);
            soapEnvelopeXml.Save(stream);


                        Console.WriteLine(stream);
                    }
       // System.Threading.Thread.Sleep(5000);
        using (WebResponse response = request.GetResponse())
                    {
                        using (StreamReader rd = new StreamReader(response.GetResponseStream()))
                        {
                            string soapResult = rd.ReadToEnd();

                            Console.WriteLine(soapResult);

                            rd.Close();

在此方法正常工作后,我会收到已处理好的命令(无需"<?xml version="1.0"?>"元素即可手动推送)

如果失败,我会收到

错误102 request_not_valid

0 个答案:

没有答案