简体   繁体   中英

Can't pass XML using WSDL and escaping characters

I'm really struggling to find a way to pass an XML string without escaping the characters in C#. I have a string below that needs to be passed into a call to a WSDL as a string.

<MyElement ElementId='xxxxxx'><Segments><Segment Status='1' SegmentId='xx1'/><Segment Status='1' 
SegmentId='xx2'/></Segments></MyElement>

If I try to pass it has a string the characters are escaping characters such as '>'. I imported the WSDL in a .NET Console application. Here is what that string above is going into as I see using the Wizdler tool in chrome.

在此处输入图像描述

The entire message is this.

在此处输入图像描述

So my question is how in C# can I use the WSDL or even just do it manually to make the call to make this work

using (var client = new MemberService.MemberWebServiceSoapClient())
 {               
   var xml = "<MyElement ElementId='xxxxxxx'><Segments><Segment Status='1' SegmentId='xx1'/><Segment 
   Status='1' SegmentId='xx2'/></Segments></MyElement>"
   var send = client.Moving(xml);
 }

Use Xml Linq:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string envelope = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +
                              "<Envelope xmlns=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
                                 "<Body>" +
                                    "<Moving xmlns=\"http://www.retalix.com/HQLWebServices\">" +
                                    "</Moving>" +
                                 "</Body>" +
                              "</Envelope>";

            XDocument doc = XDocument.Parse(envelope);
            XElement moving = doc.Descendants().Where(x => x.Name.LocalName == "Moving").FirstOrDefault();
            XNamespace ns = moving.GetDefaultNamespace();
            string elementId = "xxxxxx";
            List<Segment> segments = new List<Segment>() {
                new Segment() { id = "xx1", status = 1},
                new Segment() { id = "xx2", status = 1}
            };

            XElement xMyElement = new XElement(ns + "MyElement", new XAttribute("ElementId", elementId));
            moving.Add(xMyElement);
            XElement xSegments = new XElement(ns + "Segments");
            xMyElement.Add(xSegments);

            foreach (Segment segment in segments)
            {
                XElement xSegment = new XElement("Segment", new object[] {
                    new XAttribute("Status", segment.status),
                    new XAttribute("SegmentId", segment.id)
                });
                xSegments.Add(xSegment);
            }

            string sendXml = doc.ToString();
        }
    }
    public class Segment
    {
        public string id { get; set; }
        public int status { get; set; }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM