使用C#返回XML节点数据 [英] Return XML node data using C#

查看:115
本文介绍了使用C#返回XML节点数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在向Web服务运行SOAP请求时生成以下XML结果。



 <?  xml     version   =  1.0    encoding   =  utf-8    >  
- < SOAP-ENV:Envelope xmlns:SOAP-ENV = http://schemas.xmlsoap.org/soap/envelope/ >
- < SOAP-ENV:正文 >
- < < span class =code-leadattribute> idc:service xmlns:idc = http://www.stellent.com/IdcService/ IdcService = SEARCH_WORKFLOWS >
- < idc:文档 dUser = 用户 >
< idc:field name = SearchAuthor / >
< idc:field name = SearchCID > arf < / idc:field >
< idc:field name = SearchAssn < span class =code-attribute> / >
- < idc:resultset name = WfDocuments < span class =code-keyword>>
- < idc:row dDocName = ARF16-0711 dID = 596213 dDocType = JPS_QMARF dDocTitle = ARF16-0711 AMYWSA3377 Prop Lbl Prod PV Mat LStatn dDocAuthor < span class =code-keyword> = 作者 dRevClassID = 283640 dRevisionID = 1 dRevLabel = A0 dIsCheckedOut = 0 dCheckoutUser = dSecurityGroup = Submitted_Forms dCreateDate = 8/23/16 3:10 PM dInDate = 8/23/16 3:晚上10点 dOutDate = dStatus = REVIEW dReleaseState = E dFlag1 = dWebExtension = hcsp dProcessingState = Y dMessage = dDocAccount = dReleaseDate = dRenditi on1 = dRendition2 = dIndexerState = dPublishType = dPublishState = >
< / idc:row >
< / idc:resultset >
< / idc:document >
< / idc:service < span class =code-keyword>>
< / SOAP-ENV:正文 >
< / SOAP-ENV:信封 >





完整的SOAP调用字符串是 https:// auhjpsv01 / stellent / idcplg?IdcService = SEARCH_WORKFLOWS& SearchAssn =& SearchCID = arf& SearchAuthor =& IsSoap = 1 [ ^ ]



我想解析出来dDocName节点数据(ARF16-0711)和dCreateDate节点数据(8/23/16 3:10 PM)到文本框,但是我的代码不断出现以下错误。



 <   soap-env:信封    xmlns:soap-env   =  http://schemas.xmlsoap.org/soap/envelope/ >  
< soap-env:body >
< soap-env:fault >
< faultcode > 客户端< / faultcode >
< faultstring > Soap请求无效。服务节点'(null)'无效。< / faultstring >
< / soap-env:fault < span class =code-keyword>>

< / soap-env:正文 >
< / soap-env:信封 >





我尝试过:



这是我的代码。



< pre lang =c#> 使用系统;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.IO;
使用 System.Linq;
使用 System.Net;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows.Forms;
使用 System.Xml;

namespace SOAP_reference
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click( object sender,EventArgs e)
{
setupWebServiceSOAPCall();
}

public void setupWebServiceSOAPCall()
{
var _url = https:/ / auhjpsv01 /的Stellent / idcplg IdcService = SEARCH_WORKFLOWS&安培; SearchAssn =安培; SearchCID = ARF&安培; SearchAuthor =安培; IsSoap = 1\" ;
var _action = https:// auhjpsv01 /的Stellent / idcplg IdcService = SEARCH_WORKFLOWS&安培; SearchAssn =安培; SearchCID = ARF&安培; SearchAuthor =安培; IsSoap = 1\" ;

XmlDocument soapEnvelopeXml = CreateSOAPXMLEnvelope();
HttpWebRequest webRequest = CreateSOAPWebRequest(_url,_action);
InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml,webRequest);

// 开始异步调用Web请求。
IAsyncResult asyncResult = webRequest.BeginGetResponse( null null );

// 暂停此线程,直到调用完成。您可能希望
// 在此处执行一些有用的操作,例如更新您的UI。
asyncResult.AsyncWaitHandle.WaitOne();

// 从完成的网络请求中获取回复。
string soapResult;
使用(WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
using using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
{
soapResult = rd.ReadToEnd();
}
Console.Write(soapResult);
textBox1.Text = soapResult;
}
}

私有 静态 HttpWebRequest CreateSOAPWebRequest ( string url, string action)
{
HttpWebRequest request =(HttpWebRequest) WebRequest.Create(URL);
request.Headers.Add( SOAPAction,action);
request.ContentType = text / xml; charset = \utf-8 \< /跨度>;
request.Accept = text / xml;
request.Method = POST;
return request;
}

私有 静态 XmlDocument CreateSOAPXMLEnvelope()
{
XmlDocument envelop = new XmlDocument();
envelop.LoadXml( @ < idc:document duser =gittlera =xmlns:idc = #unknown >< idc:field name = searchauthor = />< idc:field name = searchcid = > arf< / idc:field>< idc:field name = searchassn = />< idc:resultset name = wfdocuments = >< / idc:resultset>< / idc:document>< soap-env:body xm lns:soap-env = #unknown >< / soap-env:body> );
return envelop;
}

private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml,HttpWebRequest webRequest)
{
using (Stream stream = webRequest.GetRequestStream())
{
soapEnvelopeXml.Save(stream);
}
}
}
}

解决方案

问题的措辞方式让我觉得你已经收到了来自网络服务的回复但无法提取信息。你要?如果是这样的话,这就是一种方式...



假设只读一个属性的Web服务,返回数据进行解析。

  ///   <  摘要 >  
/// Web服务返回的数据。
/// < / summary >
public static string soapData {
获取 {
return @ <?xml version =1.0encoding =utf-8?>
< SOAP-ENV:Envelope xmlns:SOAP-ENV =http:// schemas .xmlsoap.org / soap / envelope />
< SOAP-ENV:Body>
< idc:service xmlns:idc =http://www.stellent.com/ IdcService /IdcService =SEARCH_WORKFLOWS>
< idc:document dUser =user>
< idc:field name =SearchAuthor/>
< idc:field name =SearchCID> arf < / IDC:字段>
< idc:field name =SearchAssn/>
< idc:resultset name =WfDocuments>
< idc:row dDocName =ARF16-0711dID =596213
dDocType =JPS_QMARF
dDocTitle =ARF16-0711 AMYWSA3377 Prop Lbl Prod PV Mat LStatn
dDocAuthor =author
dRevClassID =283640dRevisionID =1dRevLabel =A0
dIsCheckedOut = 0dCheckoutUser =

dSecurityGroup =
Submitted_Forms
dCreateDate =
8/23/16 3:10 PM
dInDate =
8/23/16 3 :晚上10点 dOutDate =
dStatus =
REVIEW dReleaseState = E dFlag1 =
dWebExtension =
hcsp dProcessingState = Y
dMessage =
dDocAccount = dReleaseDate =
dRendition1 =
< span class =code-string> dRendition2 = dIndexerState =
dPublishType =
dPublishState = < span class =code-string> >
< / idc:row>
< / idc:resultset>
< / idc:document>
< / idc:service>
< / SOAP-ENV:正文>
< / SOAP-ENV:Envelope>
;
}
}





现在提取信息的方法。我们感兴趣。这是仅选择一种类型的节点(它将选择该类型的所有节点)并仅提取上面提到的属性的硬代码问题。

  ///   <  摘要 >  
/// 硬编码从xpath选择的单个节点读取两个属性。
/// < / summary >
/// < param < span class =code-summarycomment> name =soapmessage > < / param >
public static void read( string soapmessage){


XmlDocument document = new XmlDocument();
document.LoadXml(soapmessage); // 将肥皂信息加载为字符串

// 设置idc服务的命名空间。
// 需要允许xpath选择带前缀的节点。
// 只是为了好玩,注释掉这些行并使用
// document .SelectNodes(// idc:row)代替当前调用以查看
// 会发生什么。
XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
manager.AddNamespace( idc http://www.stellent.com/IdcService/);

XmlNodeList xnList = document.SelectNodes( // idc:row , 经理);
int nodes = xnList.Count;

foreach (XmlNode xn in xnList){
// 获取感兴趣的属性值。
/ / 属性是一个集合。
// 注意null异常,其中属性是可选的且不是
// 在节点上找到。
Console.WriteLine( Doc:{0}已创建:{ 1}
xn.Attributes [ dDocName]。 ,
xn.Attributes [ dCreateDate]。Value);
}


}





并进行测试。 ..



  ///   <  摘要 >  
/// 测试它。
/// < / summary >
public static void test(){
read(soapData);
}





输出



< pre lang =text> Doc:ARF16-0711创建时间:8/23/16 3:10 PM





LINQ for XML是另一种选择。


感谢您的邮政工作。还有一个问题,当我运行SOAP调用时拉出的XML数据有一堆字段,其中一个我需要从中提取数据..如何从idc中提取值:field name =dWfStepName?



我需要根据该字段的值过滤我从XML数据返回的列表。



我试图根据谁需要对它们执行操作来提取活动工作流列表。如果dWfStepName = Workcell,我不希望列表中有dDocName,否则如果它包含其他内容,我想要使用dDocName和dCreateDate填充列表。



<?xml version =1.0encoding =utf-8?>

- < SOAP-ENV:Envelope xmlns:SOAP-ENV =http://schemas.xmlsoap.org/soap/envelope/>

- < SOAP-ENV:正文>

- < idc:service xmlns:idc =http://www.stellent.com/IdcService/IdcService =SEARCH_WORKFLOWS>

- < idc:document dUser =user>

< i dc:field name =SearchAuthor/>

< idc:field name =SearchCID> arf< / idc:field>

< idc:field name =SearchAssn/>

- < idc:resultset name =WfDocuments>

- < idc:row dDocName =ARF16-0711dID =596213dDocType = JPS_QMARFdDocTitle =ARF16-0711 AMYWSA3377 Prop Lbl Prod PV Mat LStatndDocAuthor =authordRevClassID =283640dRevisionID =1dRevLabel =A0dIsCheckedOut =0dCheckoutUser =dSecurityGroup =Submitted_Forms dCreateDate =8/23/16 3:10 PMdInDate =8/23/16 3:10 PMdOutDate =dStatus =REVIEWdReleaseState =EdFlag1 =dWebExtension =hcspdProcessingState =YdMessage =dDocAccount =dReleaseDate =dRendition1 =dRendition2 =dIndexerState =dPublishType =dPublishState =>

< idc: field name =dWfID> 1741< / idc:field>

< idc:field name =dWfDocState> INPROCESS< / idc:field>

< idc:field name =dWfComputed/>

< idc:field name =dWfCurrentStepID> 6168< / idc:field>

< idc:field name =dWfDirectory> submitted_forms< / idc:field>

< idc:field name =dClbraName/>

< idc:field name =dWorkflowState> R< / idc:field>

< idc:field name =dRevRank> 0< / idc:field>

< idc:field name =xReferenced_By/>

< idc:field name =xNew_Assembly> No< / idc:field>

< idc:field name =xNew_Product_Containment/>

< idc:field name =xBay_Number/>

< idc:field name =xWave_Number/>

< idc:field name =xCell_Number/>

< idc:field name =xVendor/>

< idc:field name =xPlant/>

< idc:field name =xIndustry/>

< idc:field name =xAffected_Departments/>

< idc:field name =xIDCPlacement/>

< idc:field name =xProduct_Group/>

< idc:field name =xIDCWatermark>无< / idc:field>

< idc:field name =xCustomer> Amway< / idc:field>

< idc:field name =xIDCSecureChange/>

< idc:field name =xIDCSecurePrint>启用< / idc:field>

< idc:field name =xIDCSecureSelect/>

< idc:field name =xIDCSecureNotes/>

< idc:field name =xSystem_Internal/>

< idc:field name =xImpacts_EMS/>

< idc:field name =xCustomer_ECO_Number/>

< idc:field name =xDistribution/>

< idc:field name =xCustomer_Deviation/>

<idc:field name=\"xAssembly_Number\">AMYWSA3377</idc:field>

<idc:field name=\"xPart_Number\" />

<idc:field name=\"xComments\" />

<idc:field name=\"xReference_Documents\">,</idc:field>

<idc:field name=\"xworkflowRouting\" />

<idc:field name=\"xphase\" />

<idc:field name=\"xAssigned\">dawkinsd</idc:field>

<idc:field name=\"xTraining\">No</idc:field>

<idc:field name=\"xVerification\">No</idc:field>

<idc:field name=\"xValidation\">Yes</idc:field>

<idc:field name=\"temp_xcustomer_eco_number\" />

<idc:field name=\"temp_xcustomer_deviation\" />

<idc:field name=\"dWfStepName\">Workcell</idc:field>

<idc:field name=\"dWfStepID\">6168</idc:field>

<idc:field name=\"dWfStepDescription\" />

<idc:field name=\"dWfStepType\">:R:</idc:field>

<idc:field name=\"dWfStepIsAll\">0</idc:field>

<idc:field name=\"dWfStepWeight\">1</idc:field>

<idc:field name=\"dWfName\">Amway_JPS_ARF_WF</idc:field>

<idc:field name=\"dWfDescription\" />

<idc:field name=\"dCompletionDate\" />

<idc:field name=\"dWfStatus\">INPROCESS</idc:field>

<idc:field name=\"dWfType\">SubWorkflow</idc:field>

<idc:field name=\"dProjectID\" />

<idc:field name=\"dIsCollaboration\">0</idc:field>

</idc:row>

</idc:resultset>

</idc:document>

</idc:service>

</SOAP-ENV:Body>

</SOAP-ENV:Envelope>



I tried setting up

XmlNode node = xn.SelectSingleNode(\"idc:field name=’dWfStepName’\")

but kept getting the following error

'idc:field name='dWfStepName'' has an invalid token.



I apologize for asking what seems to be like simple questions. I am very new to XML. Most of my background is with LabView, C++, C# and SQL.



cigwork edit



Have a look at ... http://www.w3schools.com/xsl/xpath_syntax.asp



In this case you’re interested in selecting nodes by attributes and the value is the innertext of the node.



XmlNodeList xnList = document.SelectNodes(\"//idc:field[@name='dWfStepName']\", manager); 
int nodes = xnList.Count;

foreach (XmlNode xn in xnList) {
Console.WriteLine(\"Step Name: {0}\",
xn.InnerText);


FINAL SOLUTION:

Thanks again cigwork for your help. Now I can build a priority list and when I run the SOAP call I can adjust the priority by what has fallen off the list, what is new and what is still there.



There is more code than this (about 50K lines, and this XML piece was the final piece to the puzzle!!!).



XmlNodeList xnList = document.SelectNodes(\"//idc:row\", manager); 
int nodes = xnList.Count;
string step = null;

foreach (XmlNode xn in xnList)
{
var doc = xn.Attributes[\"dDocName\"].Value.ToString();

XmlNode WFStep = document.SelectSingleNode(\"//idc:row[@dDocName='\"+doc+\"']/idc:field[@name='dWfStepName']\", manager);

//foreach (XmlNode xn1 in xnList1)
//{
step = WFStep.InnerText.ToString();
//}

listBox1.Items.Add(\"Doc: \" + xn.Attributes[\"dDocName\"].Value.ToString()
+ \". Revision: \" + xn.Attributes[\"dRevLabel\"].Value.ToString()
+ \". Created Date: \" + xn.Attributes[\"dCreateDate\"].Value.ToString()
+ \". Status: \" + xn.Attributes[\"dStatus\"].Value.ToString()
+ \". Step: \" + step
);
}
}


I have the following XML results that are generated when I run a SOAP request to a webservice.

 <?xml version="1.0" encoding="utf-8" ?> 
- <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
- <SOAP-ENV:Body>
- <idc:service xmlns:idc="http://www.stellent.com/IdcService/" IdcService="SEARCH_WORKFLOWS">
- <idc:document dUser="user">
  <idc:field name="SearchAuthor" /> 
  <idc:field name="SearchCID">arf</idc:field> 
  <idc:field name="SearchAssn" /> 
- <idc:resultset name="WfDocuments">
- <idc:row dDocName="ARF16-0711" dID="596213" dDocType="JPS_QMARF" dDocTitle="ARF16-0711 AMYWSA3377 Prop Lbl Prod PV Mat LStatn" dDocAuthor="author" dRevClassID="283640" dRevisionID="1" dRevLabel="A0" dIsCheckedOut="0" dCheckoutUser="" dSecurityGroup="Submitted_Forms" dCreateDate="8/23/16 3:10 PM" dInDate="8/23/16 3:10 PM" dOutDate="" dStatus="REVIEW" dReleaseState="E" dFlag1="" dWebExtension="hcsp" dProcessingState="Y" dMessage="" dDocAccount="" dReleaseDate="" dRendition1="" dRendition2="" dIndexerState="" dPublishType="" dPublishState="">
</idc:row>
  </idc:resultset>
  </idc:document>
  </idc:service>
  </SOAP-ENV:Body>
  </SOAP-ENV:Envelope>



The full SOAP call string is https://auhjpsv01/stellent/idcplg?IdcService=SEARCH_WORKFLOWS&SearchAssn=&SearchCID=arf&SearchAuthor=&IsSoap=1[^]

I want to parse out the dDocName node data (ARF16-0711) and the dCreateDate node data (8/23/16 3:10 PM) to a text box, but keep getting the following error with my code.

<soap-env:envelope xmlns:soap-env="http://schemas.xmlsoap.org/soap/envelope/">
<soap-env:body>
<soap-env:fault>
<faultcode>Client</faultcode>
<faultstring>The Soap request is invalid.  The service node '(null)' is invalid.</faultstring>
</soap-env:fault>
</soap-env:body>
</soap-env:envelope>



What I have tried:

Here is my code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;

namespace SOAP_reference
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            setupWebServiceSOAPCall();            
        }

        public void setupWebServiceSOAPCall()
        {
            var _url = "https://auhjpsv01/stellent/idcplg?IdcService=SEARCH_WORKFLOWS&SearchAssn=&SearchCID=arf&SearchAuthor=&IsSoap=1";
            var _action = "https://auhjpsv01/stellent/idcplg?IdcService=SEARCH_WORKFLOWS&SearchAssn=&SearchCID=arf&SearchAuthor=&IsSoap=1";

            XmlDocument soapEnvelopeXml = CreateSOAPXMLEnvelope();
            HttpWebRequest webRequest = CreateSOAPWebRequest(_url, _action);
            InsertSoapEnvelopeIntoWebRequest(soapEnvelopeXml, webRequest);

            // begin async call to web request.
            IAsyncResult asyncResult = webRequest.BeginGetResponse(null, null);

            // suspend this thread until call is complete. You might want to
            // do something usefull here like update your UI.
            asyncResult.AsyncWaitHandle.WaitOne();

            // get the response from the completed web request.
            string soapResult;
            using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
            {
                using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
                {
                    soapResult = rd.ReadToEnd();
                }
                Console.Write(soapResult);
                textBox1.Text = soapResult;
            } 
        }

        private static HttpWebRequest CreateSOAPWebRequest(string url, string action)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Headers.Add("SOAPAction", action);
            request.ContentType = "text/xml;charset=\"utf-8\"";
            request.Accept = "text/xml";
            request.Method = "POST";
            return request;
        }

        private static XmlDocument CreateSOAPXMLEnvelope()
        {
            XmlDocument envelop = new XmlDocument();
            envelop.LoadXml(@"<idc:document duser="" gittlera="" xmlns:idc="#unknown"><idc:field name="" searchauthor="" /><idc:field name="" searchcid="">arf</idc:field><idc:field name="" searchassn="" /><idc:resultset name="" wfdocuments=""></idc:resultset></idc:document><soap-env:body xmlns:soap-env="#unknown"></soap-env:body>");
            return envelop;
        }

        private static void InsertSoapEnvelopeIntoWebRequest(XmlDocument soapEnvelopeXml, HttpWebRequest webRequest)
        {
            using (Stream stream = webRequest.GetRequestStream())
            {
                soapEnvelopeXml.Save(stream);
            }
        }
    }
}

解决方案

The way the question is worded makes me think you've already got the response from the web service but can't extract the info. you want? If so here's one way ...

Fake a call to the web service with a read only property returning the data to parse.

/// <summary>
/// The data returned by the web service.
/// </summary>
public static string soapData {
  get {
    return @"<?xml version=""1.0"" encoding=""utf-8"" ?>
      <SOAP-ENV:Envelope xmlns:SOAP-ENV=""http://schemas.xmlsoap.org/soap/envelope/"">
        <SOAP-ENV:Body>
          <idc:service xmlns:idc=""http://www.stellent.com/IdcService/"" IdcService=""SEARCH_WORKFLOWS"">
            <idc:document dUser=""user"">
              <idc:field name=""SearchAuthor"" />
              <idc:field name=""SearchCID"">arf</idc:field>
              <idc:field name=""SearchAssn"" />
              <idc:resultset name=""WfDocuments"">
                <idc:row dDocName=""ARF16-0711"" dID=""596213""
                         dDocType=""JPS_QMARF""
                         dDocTitle=""ARF16-0711 AMYWSA3377 Prop Lbl Prod PV Mat LStatn""
                         dDocAuthor=""author""
                         dRevClassID=""283640"" dRevisionID=""1"" dRevLabel=""A0""
                         dIsCheckedOut=""0"" dCheckoutUser=""""
                         dSecurityGroup=""Submitted_Forms""
                         dCreateDate=""8/23/16 3:10 PM""
                         dInDate=""8/23/16 3:10 PM"" dOutDate=""""
                         dStatus=""REVIEW"" dReleaseState=""E"" dFlag1=""""
                         dWebExtension=""hcsp"" dProcessingState=""Y""
                         dMessage="""" dDocAccount="""" dReleaseDate=""""
                         dRendition1="""" dRendition2="""" dIndexerState=""""
                         dPublishType="""" dPublishState="""">
                </idc:row>
              </idc:resultset>
            </idc:document>
          </idc:service>
        </SOAP-ENV:Body>
      </SOAP-ENV:Envelope>";
  }
}



Now a method to extract the info. we're interested in. This is hard code to select only one type of node (it'll select all nodes of that type) and to extract only the attributes mentioned in the question.

/// <summary>
/// Hard coded to read two attributes from a single node selected by xpath.
/// </summary>
/// <param name="soapmessage"></param>
public static void read(string soapmessage) {


  XmlDocument document = new XmlDocument();
  document.LoadXml(soapmessage);  //loading soap message as string

  // Set up the namespace for the idc service.
  // Need it to allow xpath selection of prefixed nodes.
  // Just for fun comment out these lines and use
  // document.SelectNodes("//idc:row") instead of the current call to see
  // what happens.
  XmlNamespaceManager manager = new XmlNamespaceManager(document.NameTable);
  manager.AddNamespace("idc", "http://www.stellent.com/IdcService/");

  XmlNodeList xnList = document.SelectNodes("//idc:row", manager);
  int nodes = xnList.Count;

  foreach (XmlNode xn in xnList) {
    // Get the attribute values of interest.
    // Attributes is a collection.
    // Beware null exception where an attribute is optional and isn't
    // found on a node.
    Console.WriteLine("Doc: {0} Created: {1}",
                      xn.Attributes["dDocName"].Value,
                      xn.Attributes["dCreateDate"].Value);
  }


}



and to test it...

/// <summary>
/// Test it.
/// </summary>
public static void test() {
  read(soapData);
}



Output

Doc: ARF16-0711 Created: 8/23/16 3:10 PM



LINQ for XML is an alternative.


Thank you for your post cigwork. One more question, the XML data that is pulled when I run the SOAP call has a bunch of fields, one of which I need to pull data from.. How do I go about pulling the value from idc:field name="dWfStepName?

I need to filter the list I am returning from the XML data based on the value of that field.

I am trying to pull a list of active workflows based on who is required to perform an action on them. If dWfStepName = Workcell, I don't want the dDocName in the list, otherwise if it contains anything else, I want to populate the list with the dDocName and dCreateDate.

<?xml version="1.0" encoding="utf-8" ?>
- <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
- <SOAP-ENV:Body>
- <idc:service xmlns:idc="http://www.stellent.com/IdcService/" IdcService="SEARCH_WORKFLOWS">
- <idc:document dUser="user">
<idc:field name="SearchAuthor" />
<idc:field name="SearchCID">arf</idc:field>
<idc:field name="SearchAssn" />
- <idc:resultset name="WfDocuments">
- <idc:row dDocName="ARF16-0711" dID="596213" dDocType="JPS_QMARF" dDocTitle="ARF16-0711 AMYWSA3377 Prop Lbl Prod PV Mat LStatn" dDocAuthor="author" dRevClassID="283640" dRevisionID="1" dRevLabel="A0" dIsCheckedOut="0" dCheckoutUser="" dSecurityGroup="Submitted_Forms" dCreateDate="8/23/16 3:10 PM" dInDate="8/23/16 3:10 PM" dOutDate="" dStatus="REVIEW" dReleaseState="E" dFlag1="" dWebExtension="hcsp" dProcessingState="Y" dMessage="" dDocAccount="" dReleaseDate="" dRendition1="" dRendition2="" dIndexerState="" dPublishType="" dPublishState="">
<idc:field name="dWfID">1741</idc:field>
<idc:field name="dWfDocState">INPROCESS</idc:field>
<idc:field name="dWfComputed" />
<idc:field name="dWfCurrentStepID">6168</idc:field>
<idc:field name="dWfDirectory">submitted_forms</idc:field>
<idc:field name="dClbraName" />
<idc:field name="dWorkflowState">R</idc:field>
<idc:field name="dRevRank">0</idc:field>
<idc:field name="xReferenced_By" />
<idc:field name="xNew_Assembly">No</idc:field>
<idc:field name="xNew_Product_Containment" />
<idc:field name="xBay_Number" />
<idc:field name="xWave_Number" />
<idc:field name="xCell_Number" />
<idc:field name="xVendor" />
<idc:field name="xPlant" />
<idc:field name="xIndustry" />
<idc:field name="xAffected_Departments" />
<idc:field name="xIDCPlacement" />
<idc:field name="xProduct_Group" />
<idc:field name="xIDCWatermark">None</idc:field>
<idc:field name="xCustomer">Amway</idc:field>
<idc:field name="xIDCSecureChange" />
<idc:field name="xIDCSecurePrint">Enable</idc:field>
<idc:field name="xIDCSecureSelect" />
<idc:field name="xIDCSecureNotes" />
<idc:field name="xSystem_Internal" />
<idc:field name="xImpacts_EMS" />
<idc:field name="xCustomer_ECO_Number" />
<idc:field name="xDistribution" />
<idc:field name="xCustomer_Deviation" />
<idc:field name="xAssembly_Number">AMYWSA3377</idc:field>
<idc:field name="xPart_Number" />
<idc:field name="xComments" />
<idc:field name="xReference_Documents">,</idc:field>
<idc:field name="xworkflowRouting" />
<idc:field name="xphase" />
<idc:field name="xAssigned">dawkinsd</idc:field>
<idc:field name="xTraining">No</idc:field>
<idc:field name="xVerification">No</idc:field>
<idc:field name="xValidation">Yes</idc:field>
<idc:field name="temp_xcustomer_eco_number" />
<idc:field name="temp_xcustomer_deviation" />
<idc:field name="dWfStepName">Workcell</idc:field>
<idc:field name="dWfStepID">6168</idc:field>
<idc:field name="dWfStepDescription" />
<idc:field name="dWfStepType">:R:</idc:field>
<idc:field name="dWfStepIsAll">0</idc:field>
<idc:field name="dWfStepWeight">1</idc:field>
<idc:field name="dWfName">Amway_JPS_ARF_WF</idc:field>
<idc:field name="dWfDescription" />
<idc:field name="dCompletionDate" />
<idc:field name="dWfStatus">INPROCESS</idc:field>
<idc:field name="dWfType">SubWorkflow</idc:field>
<idc:field name="dProjectID" />
<idc:field name="dIsCollaboration">0</idc:field>
</idc:row>
</idc:resultset>
</idc:document>
</idc:service>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

I tried setting up

XmlNode node = xn.SelectSingleNode("idc:field name='dWfStepName'")

but kept getting the following error

'idc:field name='dWfStepName'' has an invalid token.


I apologize for asking what seems to be like simple questions. I am very new to XML. Most of my background is with LabView, C++, C# and SQL.

cigwork edit

Have a look at ... http://www.w3schools.com/xsl/xpath_syntax.asp

In this case you're interested in selecting nodes by attributes and the value is the innertext of the node.

XmlNodeList xnList = document.SelectNodes("//idc:field[@name='dWfStepName']", manager);
     int nodes = xnList.Count;

     foreach (XmlNode xn in xnList) {
       Console.WriteLine("Step Name: {0}",
                         xn.InnerText);


FINAL SOLUTION:
Thanks again cigwork for your help. Now I can build a priority list and when I run the SOAP call I can adjust the priority by what has fallen off the list, what is new and what is still there.

There is more code than this (about 50K lines, and this XML piece was the final piece to the puzzle!!!).

XmlNodeList xnList = document.SelectNodes("//idc:row", manager);
            int nodes = xnList.Count;
            string step = null;

            foreach (XmlNode xn in xnList)
            {
                var doc = xn.Attributes["dDocName"].Value.ToString();

                XmlNode WFStep = document.SelectSingleNode("//idc:row[@dDocName='"+doc+"']/idc:field[@name='dWfStepName']", manager);
                 
                 //foreach (XmlNode xn1 in xnList1)
                 //{
                     step = WFStep.InnerText.ToString();
                 //}

                listBox1.Items.Add("Doc: " + xn.Attributes["dDocName"].Value.ToString() 
                    + ". Revision: " + xn.Attributes["dRevLabel"].Value.ToString() 
                    + ". Created Date: " + xn.Attributes["dCreateDate"].Value.ToString() 
                    + ". Status: " + xn.Attributes["dStatus"].Value.ToString() 
                    + ". Step: " + step
                    );
            }
        }


这篇关于使用C#返回XML节点数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆