2012-07-19 26 views
0

我正嘗試讀取由ASP腳本創建的動態xml,但不幸的是我無法讀取。看來我的C#代碼無法理解ASP腳本。從動態asp腳本讀取動態xml

我很樂意聽到一些建議。

你可以看到ASP腳本的位置:http://www.ad-net.co.il/test.asp

<% 
Response.Buffer = True 
Response.ContentType = "text/xml" 
body = "" 
body = body & "<?xml version=""1.0"" encoding=""utf-8""?>" 
body = body & "<PROGRAM>" 
body = body & "<EMAIL>[email protected]</EMAIL>" 
body = body & "<USERNAME>[email protected]</USERNAME>" 
body = body & "<PASSWORD>password</PASSWORD>" 
body = body & "</PROGRAM>" 
Response.Write(body) 
%> 

的C#代碼:

private static void LoadXmlFromServerToProgram() 
    { 
     try 
     { 
      XmlDocument xDoc = new XmlDocument(); 
      xDoc.Load("http://www.ad-net.co.il/test.asp"); 

      EMAIL = xDoc.DocumentElement.SelectSingleNode("EMAIL").InnerText; 
      USERNAME = xDoc.DocumentElement.SelectSingleNode("USERNAME").InnerText; 
      PASSWORD = xDoc.DocumentElement.SelectSingleNode("PASSWORD").InnerText;     
     } 
     catch 
     { 
      MessageBox.Show("Can't Read From XML"); 
     } 
    } 
+0

能否請您提供詳細信息, ...)。另外請考慮製作變量名稱camelCase,而不是ALL-UPPER-CASE。 – 2012-07-19 23:03:40

+0

您的示例C#代碼在新的控制檯應用程序中完美工作。 (將3個變量聲明爲字符串)。您是否使用過Visual Studio調試器來遍歷代碼? http://imgur.com/m57JL – 2012-07-19 23:04:57

+0

我已經聲明它是一個字符串,但仍然不會工作 – 2012-07-19 23:12:06

回答

1

不應該使用的SelectSingleNode這樣..你需要一個XPath查詢,而不是單個頂級節點。 Look here for information on XPath

你需要嘗試是這樣的:

EMAIL = xDoc.CreateNavigator().SelectSingleNode("/PROGRAM/EMAIL").Value; 
+0

我試過了,但不幸的是它仍然沒有工作,謝謝你的幫助,有沒有其他建議? – 2012-07-19 23:02:40

+0

什麼不工作?問題在哪裏? – 2012-07-19 23:05:08

+0

我仍然收到MessageBox(「無法從XML讀取」),它似乎無法將其作爲XML文件 – 2012-07-19 23:06:06

0

試試這個:你有(異常信息,或者是意外的行爲,你觀察問題

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Xml; 

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

     private void Form1_Load(object sender, EventArgs e) 
     { 
      Form1.LoadXmlFromServerToProgram(); 

      MessageBox.Show(string.Concat("Email: ", Email, "\r\n", "UserName: ", UserName, "\r\n", "Password: ", Password)); 
     } 

     public static string Email { get; set; } 
     public static string UserName { get; set; } 
     public static string Password { get; set; } 

     private static void LoadXmlFromServerToProgram() 
     { 
      try 
      { 
       XmlDocument xDoc = new XmlDocument(); 
       xDoc.Load("http://www.ad-net.co.il/test.asp"); 

       Email = xDoc.DocumentElement.SelectSingleNode("EMAIL").InnerText; 
       UserName = xDoc.DocumentElement.SelectSingleNode("USERNAME").InnerText; 
       Password = xDoc.DocumentElement.SelectSingleNode("PASSWORD").InnerText; 
      } 
      catch 
      { 
       MessageBox.Show("Can't Read From XML"); 
      } 
     } 
    } 

    partial class Form1 
    { 
     /// <summary> 
     /// Required designer variable. 
     /// </summary> 
     private System.ComponentModel.IContainer components = null; 

     /// <summary> 
     /// Clean up any resources being used. 
     /// </summary> 
     /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
     protected override void Dispose(bool disposing) 
     { 
      if (disposing && (components != null)) 
      { 
       components.Dispose(); 
      } 
      base.Dispose(disposing); 
     } 

     #region Windows Form Designer generated code 

     /// <summary> 
     /// Required method for Designer support - do not modify 
     /// the contents of this method with the code editor. 
     /// </summary> 
     private void InitializeComponent() 
     { 
      this.SuspendLayout(); 
      // 
      // Form1 
      // 
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
      this.ClientSize = new System.Drawing.Size(284, 262); 
      this.Name = "Form1"; 
      this.Text = "Form1"; 
      this.Load += new System.EventHandler(this.Form1_Load); 
      this.ResumeLayout(false); 

     } 

     #endregion 
    } 
} 
+0

OMG,我不明白你在這裏寫了什麼,Iv'e試圖運行你的腳本,但我得到了這麼多的錯誤,所以如果你可以使它更多更簡單我會很高興... – 2012-07-19 23:38:55

+0

你說你使用winforms。所以,我創建了一個新的Windows窗體應用程序項目,並使用窗體(Form1)中的一些代碼。您看到的代碼是整個Form1.cs(公共部分類Form1:Form ...)和Form1.Designer.cs(部分類Form1)。 – 2012-07-20 01:17:38

+0

更簡單,這裏是您可以下載的項目。 http://silentblood.lexonian.com/TestApplication2.zip。下載它時,請告訴我。 – 2012-07-20 01:22:03