C# 使用XmlDictionaryReader从消息中获取值
我需要从我的WCF消息中获取一个值。“我的消息”在调试器中具有以下值:C# 使用XmlDictionaryReader从消息中获取值,c#,.net,xml,wcf,C#,.net,Xml,Wcf,我需要从我的WCF消息中获取一个值。“我的消息”在调试器中具有以下值: <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Header> <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IB
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IBrokerService/SaveAndPrint</Action>
</s:Header>
<s:Body>
<SaveAndPrint xmlns="http://tempuri.org/">
<contract xmlns:d4p1="http://schemas.datacontract.org/2004/07/BrokerService.Contracts" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:ContainerHistoryContracts i:nil="true" />
<!--Lots of nodes removed for brevity-->
<d4p1:CurrentBagId>123456</d4p1:CurrentBagId>
<!--Lots more nodes removed for brevity-->
<d4p1:WorkStation>TheNeededValue</d4p1:WorkStation>
</contract>
</SaveAndPrint>
</s:Body>
</s:Envelope>
试试xml linq
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication6
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
//using unique keys
Dictionary<string, string> dict1 = doc.Descendants().Where(x => x.Name.LocalName == "contract").FirstOrDefault().Elements()
.GroupBy(x => x.Name.LocalName, y => ((string)y).Trim())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
//when there are duplicate keys
Dictionary<string, List<string>> dict2 = doc.Descendants().Where(x => x.Name.LocalName == "contract").FirstOrDefault().Elements()
.GroupBy(x => x.Name.LocalName, y => ((string)y).Trim())
.ToDictionary(x => x.Key, y => y.ToList());
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序6
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
//使用唯一键
Dictionary dict1=doc.subjects().Where(x=>x.Name.LocalName==“contract”).FirstOrDefault().Elements()
.GroupBy(x=>x.Name.LocalName,y=>((字符串)y).Trim())
.ToDictionary(x=>x.Key,y=>y.FirstOrDefault());
//当存在重复的密钥时
Dictionary dict2=doc.subjects().Where(x=>x.Name.LocalName==“contract”).FirstOrDefault().Elements()
.GroupBy(x=>x.Name.LocalName,y=>((字符串)y).Trim())
.ToDictionary(x=>x.Key,y=>y.ToList());
}
}
}
您的XML解析代码是什么样子的?@DVK-不确定我的坏代码会有什么帮助,但我已经添加了它。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication6
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
//using unique keys
Dictionary<string, string> dict1 = doc.Descendants().Where(x => x.Name.LocalName == "contract").FirstOrDefault().Elements()
.GroupBy(x => x.Name.LocalName, y => ((string)y).Trim())
.ToDictionary(x => x.Key, y => y.FirstOrDefault());
//when there are duplicate keys
Dictionary<string, List<string>> dict2 = doc.Descendants().Where(x => x.Name.LocalName == "contract").FirstOrDefault().Elements()
.GroupBy(x => x.Name.LocalName, y => ((string)y).Trim())
.ToDictionary(x => x.Key, y => y.ToList());
}
}
}