【问题标题】:I used SvcUtil to create classes from WSDL. What do I deserialize into?我使用 SvcUtil 从 WSDL 创建类。我要反序列化成什么?
【发布时间】:2019-10-21 02:33:08
【问题描述】:

我使用 svcUtil.exe 从 https://gw.sam.gov/SAMWS/1.0/Entity?wsdl 创建类

但是我一辈子都找不到将结果反序列化成什么?当我创建自己的类时,根是信封,但它甚至不在新类中。

我可以粘贴课程,但它真的很长吗?有没有一个通用的答案?

我会根据要求粘贴课程。

提前谢谢...

课程长度超过 10 倍,无法发布。

为拉取添加代码:

static void Main(string[] args)
        {
            ServicePointManager.Expect100Continue = true;
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 



            var _url = "https://gw.sam.gov/SAMWS/1.0/Entity";

            //Run date is a specific date if provided otherwise use yesterday
            DateTime startDateTime = DateTime.Now.AddDays(-1).Date;

            for (int hr = 0; hr < 24; hr++)
            {
                XMLclassRequest xmlSoap = new XMLclassRequest();
                string soap = xmlSoap.BuildSOAPrequest(startDateTime.AddHours(hr));
                //string soap2 = xmlSoap.BuildSOAPrequest2(startDateTime.AddHours(hr));

                string response = null;   //This is the original pull with FAR and DFAR Responses
                //string response2 = null;  //This is FAR and DFAR


                using (MyWebClient client = new MyWebClient())
                {
                    client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
                    client.Headers.Add("SOAPAction", "\"http://tempuri.org/IMyService/MyOperation\"");
                    try
                    {
                        response = client.UploadString(_url, soap);
                        //response2 = client.UploadString(_url, soap2);
                    }
                    catch
                    { } //This will skip the hour attempted and move to next. The error I have been receiving is no data which is differently formatted XML that causes the error

                }

                //File.WriteAllText(@"D:\temp\bigpull.xml", response);

                MemoryStream stream = null;
                if (response != null)
                {

                    byte[] byteArray = Encoding.Unicode.GetBytes(response);
                    stream = new MemoryStream(byteArray);
                }


                getEntities results;
                XmlSerializer serializer = new XmlSerializer(typeof(getEntities));
                try
                { results = (getEntities)serializer.Deserialize(stream); }
                catch
                { } //This will skip the hour attempted and move to next. The error I have been receiving is no data which is differently formatted XML that causes the error
                stream.Close();

                string str;
            }

回复回来了。我只是无法将其放入要使用的对象中。

【问题讨论】:

  • 嗯,通常它会为您需要进行的调用以及您发送/返回的数据类型创建类..
  • 我以前从未使用过 svcutil。请求是 getEntities 让我看看是否存在
  • 当我试图反序列化 xml 文档 (1,2) 中的错误时,我的 catch 中出现错误
  • 取决于您所做的事情,您实际上并没有进行反序列化,它作为 xml 发送,但如果您作为服务连接到它,它会为您处理 PS 没有使用该特定集播放但通常它会返回解析后的 xml 的对象
  • 作为服务连接的问题是我需要使用 VS2012 在代码中声明 SSL 加密。如果我能解决这个问题,我会很高兴的!

标签: c# xml visual-studio-2012 deserialization svcutil.exe


【解决方案1】:

我无法将此加载到对象中,但最终将其加载到 XDocument 并解析:

    var _url = "https://gw.sam.gov/SAMWS/1.0/Entity";

    //Run date is a specific date if provided otherwise use yesterday
    DateTime startDateTime = DateTime.Now.AddDays(-1).Date;

    for (int hr = 0; hr < 24; hr++)
    {
        XMLclassRequest xmlSoap = new XMLclassRequest();
        string soap = xmlSoap.BuildSOAPrequest(startDateTime.AddHours(hr));
        //string soap2 = xmlSoap.BuildSOAPrequest2(startDateTime.AddHours(hr));

        string response = null;   //This is the original pull with FAR and DFAR Responses
        //string response2 = null;  //This is FAR and DFAR


        using (MyWebClient client = new MyWebClient())
        {
            client.Headers.Add("Content-Type", "text/xml;charset=utf-8");
            client.Headers.Add("SOAPAction", "\"http://tempuri.org/IMyService/MyOperation\"");
            try
            {
                response = client.UploadString(_url, soap);
                //response2 = client.UploadString(_url, soap2);
            }
            catch
            { } //This will skip the hour attempted and move to next. The error I have been receiving is no data which is differently formatted XML that causes the error

        }

        //File.WriteAllText(@"D:\temp\far20190604.xml", response);

        XDocument xdoc = XDocument.Parse(response);

        var entities = from e in xdoc.Descendants("entity") select e;

        foreach (var e in entities)
        { 
            string DUNS = e.Descendants("DUNS").FirstOrDefault().Value;
            var provisions = from p in e.Descendants("provision") select p;

            foreach (var p in provisions)
            {
                string ParentID = p.Descendants("id").FirstOrDefault().Value;
                var answers = from a in p.Descendants("answer") select a;

                foreach (var a in answers)
                {
                    var section = a.Descendants("section").Count()>0?  a.Descendants("section").FirstOrDefault().Value : "";
                    var answerText = a.Descendants("answerText").Count() > 0 ? a.Descendants("answerText").FirstOrDefault().Value : "";

                    Console.WriteLine(DUNS + " " + ParentID + " " + section + " " + answerText);

                }
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-01-28
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    相关资源
    最近更新 更多