C#使用字典为SOAP信封分配值?

时间:2018-05-12 00:04:48

标签: c# soap

我觉得这可能是一个非常基本的问题,但我是SOAP的新关联名称空间等,所以这里有:

我有一个SOAP请求看起来像(为了简洁而删除了完整请求):

<soapenv:Body>
  <ns:addLine sequence="?">
     <line>
        <pattern></pattern>
        <description></description>
        <usage></usage>
        <routePartitionName uuid="?"></routePartitionName>
        <callForwardAll>
              <forwardToVoiceMail></forwardToVoiceMail>
           <callingSearchSpaceName uuid="?"></callingSearchSpaceName>
           <secondaryCallingSearchSpaceName uuid="?"></secondaryCallingSearchSpaceName>
              <destination></destination>
        </callForwardAll>
        <callForwardBusy>
              <forwardToVoiceMail></forwardToVoiceMail>
           <callingSearchSpaceName uuid="?"></callingSearchSpaceName>
              <destination></destination>
        </callForwardBusy>
        <callForwardBusyInt>
              <forwardToVoiceMail></forwardToVoiceMail>
           <callingSearchSpaceName uuid="?"></callingSearchSpaceName>
              <destination></destination>
        </callForwardBusyInt>

这些元素的值将从app.config和用户在表单上输入的内容的组合中提取,我认为字典将是将值分配给元素的最直接方式。

所以这是我的问题;当每个元素可以具有不同的值时,如何为“forwardToVoiceMail”和“secondaryCallingSearchSpaceName”元素赋值?我的意思是,字典只有两个维度,即键和值,因此我不能在字典中使用callForwardAll\forwardToVoiceMail, truecallForwardBusy\forwardToVoiceMail, false

任何帮助/建议都将不胜感激!

1 个答案:

答案 0 :(得分:0)

正如你所提到的那样,

像分层数据结构这样的XML对于将它与词典联系起来似乎很麻烦。另一种方法是创建类似于XML布局的类。所以哟创造:

` public class ClassForward
     bool ForwardToVoiceMail;
     GUID callingSearchSpaceName;
     GUID secondaryCallingSearchSpaceName;
     string Destination;
  end;

  public class Line
    string pattern;
    string description;
    ClassForward callForwardAll;
    ClassForward callForwardBusy;
    ClassForward callForwardBusyInt;
    ...
 `

然后使用以下内容在内存中使用XmlTextWriter和XmlTextReader进行保存和读取:

StringBuilder builder = new StringBuilder();
XmlWriter writer = XmlWriter.Create(builder, new XmlWriterSettings()));
SaveLines(ref writer, Lines);

其中:

public void SaveLines(ref XmlTextWriter writer, List<Line> myLines)
{
    foreach(Line line in MyLines)
    {
       writer.WriteStartElement("line");
       writer.Add(new XElement("pattern", line.pattern));
       writer.Add(new XElement("description", line.description));
       writert.Add(new XElement("CallForwardAll", 
                   new XElement("forwardToVoiceMail", line.forwardToVoiceMail));

...         }

你明白了。