在XML Reader中找到相关元素c#

时间:2017-07-13 14:39:24

标签: c# xml

我基本上有一个xml阅读器,每个Node都是一个带有某些元素Uid,名字等的摄像头。我试图找到一种能够输入名称并找到相关uid的方法。

到目前为止,这是我的代码:

 using (XmlReader reader = XmlReader.Create(@"\\IPADDRESS\Customers\customers\GRACE-SVR1\XmlConfig\EXAMPLE.XML"))
        {
            while (reader.Read())
            {
                if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "CAMERA"))
                {
                    if (reader.HasAttributes)
                    {
                        Console.WriteLine(reader.GetAttribute("name"));
                        Console.WriteLine(reader.GetAttribute("uid"));

                    }
                }
            }
        }
        Console.ReadLine();

这显示了信息,但我只想知道如何输入名称,并在xml中搜索相关的uid

2 个答案:

答案 0 :(得分:1)

我想,你正在寻找这个。

using System;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace DocuSignDotNet40
{
  public partial class SignatureForm : Form
  {
    protected const string _username = ""; //Enter Username here
    protected const string _integratorKey = ""; //Enter Integrator Key here
    protected const string _password = ""; //Enter Password here
    protected const string _baseUri = "https://demo.docusign.net/restapi"; //Enter baseUri here
    protected const string _accountId = ""; //Enter acountId here
    protected string _authHeader = "";

    private void CreateEnvelope_Click(object sender, EventArgs e)
    {
      string requestUrl = _baseUri + "/v2/accounts/" + _accountId + "/envelopes";
      var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
      httpWebRequest.Method = "Post";
      httpWebRequest.ContentType = "application/json";
      httpWebRequest.Headers.Add("X-DocuSign-Authentication", _authHeader);

      string jsonRequest = GetCreateEnvelopeJson();
      var data = Encoding.ASCII.GetBytes(jsonRequest);
      using (var stream = httpWebRequest.GetRequestStream())
      {
          stream.Write(data, 0, data.Length);
      }
      var response = (HttpWebResponse)httpWebRequest.GetResponse();
      using (StreamReader sr = new StreamReader(response.GetResponseStream()))
      {
          string responseJson = sr.ReadToEnd();
          // more stuff
      }
    }

    public string GetCreateEnvelopeJson()
    {
      return @"{
        'emailSubject': 'Please sign the agreement',
        'status': 'sent',
           'recipients': {
               'signers': [
                   {
                       'email': 'janedoe@acme.com',
                       'name': 'jane doe',
                       'recipientId': 1,
                       'tabs': {
                           'signHereTabs': [
                               {
                                   'documentId': '1',
                                   'pageNumber': '1',
                                   'xPosition': '80',
                                   'yPosition': '80',
                               }
                           ]
                       }
                   }
               ]
           },
           'documents': [
               {
                   'documentId': '1',
                   'name': 'Contract',
                   'fileExtension': 'txt',
                   'documentBase64': 'RG9jIFRXTyBUV08gVFdP'
               }
           ]
       }";
    }
    public void GetEnvelope()
    {
        string envelopeId = "";//Enter EnvelopeId here
        if(string.IsNullOrWhiteSpace(envelopeId)) throw new ApplicationException("Invalid Envelope Id");

        string requestUrl = _baseUri + "/v2/accounts/" + _accountId +"/envelopes/" + envelopeId ;
        var httpWebRequest = (HttpWebRequest)HttpWebRequest.Create(requestUrl);
        httpWebRequest.Method = "Get";
        httpWebRequest.Headers.Add("X-DocuSign-Authentication", _authHeader);
        var response = (HttpWebResponse)httpWebRequest.GetResponse();
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            string responseJson = sr.ReadToEnd();
            // more stuff
        }

    }

    public SignatureForm()
    {
      InitializeComponent();

      if (string.IsNullOrWhiteSpace(_username)) throw new ApplicationException("Invalid _username");
      if (string.IsNullOrWhiteSpace(_integratorKey)) throw new ApplicationException("Invalid _integratorKey");

      if (string.IsNullOrWhiteSpace(_password)) throw new ApplicationException("Invalid _password");
      if (string.IsNullOrWhiteSpace(_baseUri)) throw new ApplicationException("Invalid _baseUri");
      if (string.IsNullOrWhiteSpace(_accountId)) throw new ApplicationException("Invalid _accountId");
      _authHeader = "{\"Username\":\"" + _username + "\", \"Password\":\"" + _password + "\", \"IntegratorKey\":\"" + _integratorKey + "\"}";
    }       
  }
}

答案 1 :(得分:0)

您可以使用XDocument.Load加载XML并获取类型' Camera'的所有元素。使用LINQ查询