C#中的后代没有给出正确的价值

bav*_*avs 0 c# xml linq-to-xml

我是非常新的XML使用C#我有一个XML我需要通过父亲中的特定子项我需要获取id和调用变量变量我这样做但每次都没有通过循环

我需要通过xml的所有父项,直到我得到我想要的树吗?

xml

<message xmlns="jabber:client" to="1072@finesse1.dcloud.cisco.com" id="/finesse/api/User/1072/Dialogs__1072@finesse1.dcloud.cisco.com__104Y2" from="pubsub.finesse1.dcloud.cisco.com">
<event xmlns="http://jabber.org/protocol/pubsub#event">
 <items node="/finesse/api/User/1072/Dialogs">
  <item id="460c2d27-c914-4c24-a95f-edf9f8df45c21535">
    <notification xmlns="http://jabber.org/protocol/pubsub">
      <Update>
        <data>
          <dialogs>
            <Dialog>
              <associatedDialogUri></associatedDialogUri>
              <fromAddress>1071</fromAddress>
              <id>18639330</id>
              <mediaProperties>
                <DNIS>1072</DNIS>
                <callType>AGENT_INSIDE</callType>
                <dialedNumber>1072</dialedNumber>
                <outboundClassification></outboundClassification>
                <callvariables>
                  <CallVariable>
                    <name>callVariable1</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable2</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable3</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable4</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable5</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable6</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable7</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable8</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable9</name>
                    <value></value>
                  </CallVariable>
                  <CallVariable>
                    <name>callVariable10</name>
                    <value></value>
                  </CallVariable>
                </callvariables>
              </mediaProperties>
              <mediaType>Voice</mediaType>
              <participants>
                <Participant>
                  <actions>
                    <action>ANSWER</action>
                  </actions>
                  <mediaAddress>1072</mediaAddress>
                  <mediaAddressType>AGENT_DEVICE</mediaAddressType>
                  <startTime>2017-09-15T19:23:36.872Z</startTime>
                  <state>ALERTING</state>
                  <stateCause></stateCause>
                  <stateChangeTime>2017-09-15T19:23:36.872Z</stateChangeTime>
                </Participant>
                <Participant>
                  <actions>
                    <action>UPDATE_CALL_DATA</action>
                    <action>DROP</action>
                  </actions>
                  <mediaAddress>1071</mediaAddress>
                  <mediaAddressType>AGENT_DEVICE</mediaAddressType>
                  <startTime>2017-09-15T19:23:36.609Z</startTime>
                  <state>INITIATED</state>
                  <stateCause></stateCause>
                  <stateChangeTime>2017-09-15T19:23:36.819Z</stateChangeTime>
                </Participant>
              </participants>
              <state>ALERTING</state>
              <toAddress>1072</toAddress>
              <uri>/finesse/api/Dialog/18639330</uri>
            </Dialog>
          </dialogs>
        </data>
        <event>POST</event>
        <requestId></requestId>
        <source>/finesse/api/User/1072/Dialogs</source>
      </Update>
    </notification>
  </item>
 </items>
</event>
</message>
Run Code Online (Sandbox Code Playgroud)

这就是守则

XElement xmlroots = XElement.Parse(parsingNewXML);
//Dialog = xmlroots.Element("Dialog").Value;
var CallVariable = parsingNewXML.Contains("CallVariable");
var startCall = parsingNewXML.Contains("ALERTING");

if (CallVariable == true && startCall == true)
{

    foreach (XElement callID in xmlroots.Descendants("Dialog"))
    {
        DialogID = callID.Element("id").Value;
        //System.Windows.MessageBox.Show(DialogID);
        System.Windows.Application.Current.Dispatcher.BeginInvoke(
        DispatcherPriority.Background,
        new Action(() => ((MainWindow)System.Windows.Application.Current.MainWindow).answerCall.Visibility = Visibility.Visible));
    }
    foreach (XElement callVariables in xmlroots.Descendants("CallVariables"))
    {
        foreach (XElement callVariable in xmlroots.Descendants("CallVariable"))
        {
            list = callVariable.Element("value").Value;
        }
    }
   // state = second.Element("state").Value;
}
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 5

第一个问题是,你只是打电话Descendants("Dialog"),Descendants("CallVariables")等那些寻求在全局命名空间的元素-但你正在寻找的元素实际上是在一个命名空间中http://jabber.org/protocol/pubsub,由于这样的:

<notification xmlns="http://jabber.org/protocol/pubsub">
Run Code Online (Sandbox Code Playgroud)

当您看到xmlns="..."为所有后代设置默认命名空间时.该默认值可以由指定命名空间的元素名称显式覆盖 - 或者可以由另一个后代更改xmlns=....(您的文档包含多个默认级别.)

幸运的是,LINQ to XML可以很容易地指定名称空间,因为隐式转换stringXNamespace,以及XName +(XNamespace, string)运算符:

XDocument doc = XDocument.Parse(...);
XNamespace pubsub = "http://jabber.org/protocol/pubsub";
// Find all the descendants with a local name of "Dialog" in the
// namespace specified by the pubsub variable
foreach (XElement dialog in doc.Descendants(pubsub + "Dialog"))
{
    ...
}
Run Code Online (Sandbox Code Playgroud)

作为第二个问题,请看第二个循环:

foreach (XElement callVariables in xmlroots.Descendants("CallVariables"))
{
    foreach (XElement callVariable in xmlroots.Descendants("CallVariable"))
    {
        list = callVariable.Element("value").Value;
    }
}
Run Code Online (Sandbox Code Playgroud)

这有三个问题:

  • 您的文档中没有任何元素被称为CallVariables- callvariables而是.XML区分大小写.
  • 我敢肯定,你不想要的组合每一个呼叫变量,每个通话的变量因素.相反,我希望有类似的东西:

    foreach (var callVariables in doc.Descendants(pubsub + "callvariables"))
    {
        // Note use of Elements, not Descendants. You still need
        // the namespace part though...
        foreach (var callVariable in callVariables.Elements(pubsub + "CallVariable"))
        {
            // Do what you want
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  • 目前,您只是替换list循环体中的变量,这意味着只有循环的最后一次迭代才真正有用.

代码中可能还有许多其他问题 - 解析XML然后检查字符串表示是否包含特定字符串(而不是检查特定元素是否存在)似乎很奇怪但是这些应该可以解决开始.