【问题标题】:Parsing SOAP XML with XDocument使用 XDocument 解析 SOAP XML
【发布时间】:2016-11-17 19:22:48
【问题描述】:

我使用遗留系统(不是我的设计),它有一些数据作为序列化 xml (SOAP) 保存在 db 中。

XML 是用 SoapFormatter 创建的。

SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(stream, o);

由于版本控制问题,我不能简单地从 SOAP 反序列化。创建数据的模型已更改,并且无法对持久数据(在 xml 中)进行反序列化。所以我试图弄清楚如何将旧 SOAP 手动“反序列化”为现有模型。

我可以将它加载到 XDocument 中,然后使用 LINQ 提取节点值。但是我对 Guid 值有疑问。它们存储在 SOAP XML 中,如下所示:

<someId xsi:type="a2:Guid" xmlns:a2="http://schemas.microsoft.com/clr/ns/System">
                <_a>1396006029</_a>
                <_b>2720</_b>
                <_c>20328</_c>
                <_d>162</_d>
                <_e>217</_e>
                <_f>181</_f>
                <_g>57</_g>
                <_h>113</_h>
                <_i>92</_i>
                <_j>64</_j>
                <_k>35</_k>
            </someId>

有什么方法可以不痛吗?

我可以这样一一阅读:

var someId = xdoc.Descendants(ns + "root").Elements("someId").Elements("_a").Value;

并尝试解析为 Guid,但它看起来不太好。

谢谢,

迈克

【问题讨论】:

    标签: c# xml soap xml-serialization legacy


    【解决方案1】:
    I added <Root xmlns:xsi="abc"> to xml because the xsi namespace wan't defined.
    

    试试下面的代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace ConsoleApplication28
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                Dictionary<string, int> dict = new Dictionary<string, int>();
                XDocument doc = XDocument.Load(FILENAME);
                XElement someId = doc.Descendants("someId").FirstOrDefault();
    
                dict = someId.Elements().GroupBy(x => x.Name.LocalName, y => (int)y)
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
    
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-21
      • 2011-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多