将XML从文件传递到WCF:作为变量加载还是作为字符串传递?

时间:2012-02-24 22:27:45

标签: xml wcf c#-4.0 wcf-client xmlreader

好的,我已经找到了如何使用WCF使用Web服务。 我正在使用XMLReader将XML数据读入变量。 然后我使用服务方法调用来传递变量。但是,我收到警告说“检测到无法访问的代码”。 我正在使用Visual Web Developer 2010 Express和C#应用程序。

我希望变量可以在XMLreader切换案例之外访问。谁能告诉我怎么做?或者你能告诉我一个更好用的例子吗?我一直在寻找,但找不到一个很好的教程,将文件中的XML作为字符串与WCF消费相结合。 谢谢!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using System.Xml;
using System.Net;
using System.Threading;
using System.Data;
using System.IO;
using System.Drawing;
using System.ComponentModel;
using System.Configuration;
using System.Xml.Linq;
using ws = wsCallData;



public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
    string FName; string LName; string Email;


    ws.AuthenHead header = new ws.AuthenHead();
    header.Username = ""; header.Password = "";

    ws.Importer myService = new ws.Importer();
    myService.AuthenHead = header;

    ws.SoapClient serv = new ws.SoapClient();

    string path = "C:\\Websites\\SOAPClient\\order.xml";

    using (XmlReader reader = XmlReader.Create(path))
    {
        while (reader.Read())
        {
            // Only detect start elements.
            if (reader.IsStartElement())
            {
                // Get element name and switch on it.
                switch (reader.Name)
                {
                    case "FName":
                        // Detect this element.
                        //Console.WriteLine("Start <perls> element.");
                        if (reader.Read())
                        {
                            FName = reader.Value.Trim();
                            lbNodes.Items.Add(FName);
                            myService.FName = FName;
                        }
                        break;
                    case "LName":
                        // Detect this article element.
                        // Next read will contain text.
                        if (reader.Read())
                        {
                            //Console.WriteLine("  Text node: " + reader.Value.Trim());
                            //MessageBox.Show("LName valude: " + reader.Value.Trim());
                            LName = reader.Value.Trim();
                            lbNodes.Items.Add(LName);
                            myService.LName = LName;

                        }
                        break;
                    case "Email":
                        // Detect this element.
                        if (reader.Read())
                        {
                            Email = reader.Value.Trim();
                            lbNodes.Items.Add(Email);
                            myService.Email = Email;
                        }
                        break;



                        myService.AuthenHead = header;
                        header = myService.AuthenHead;
                        FName = myService.FName;
                        LName = myService.LName;
                        Email = myService.Email;


                        lbNodes.Items.Add("TEST: " + FName + " " + LName);
                        string response;
                        response = serv.ImportLead(header, FName, LName, Email);

                        Label1.Text = response;

                        break;

                }

            }



        }
        reader.Close();

       //To DELETE the XML file after successful transmission uncomment the following line
        //File.Delete(path);

这已更新并正在运行。我现在收到用户名和密码不正确的回复。直到我从网络服务公司那里听到有关正确凭据的消息,我想我已经尽力了。

感谢您的建议!

0 个答案:

没有答案