我在为网站应用程序使用函数时遇到问题,该函数获取xml文件的url及其对应的xsd文件的url,并根据xsd验证xml文件。
虽然当我在我的2个文件上测试时,web方法有效,但在编写按钮的功能时,我得到了(非静态字段或方法需要对象引用。)
如果我尝试将验证方法设置为静态,那么它不会显示在我的网络应用程序中使用,并且在我测试Web服务时它不会显示。
如果我需要创建一个对象的实例,我不太清楚如何正确地做到这一点。感谢任何帮助,我无法从有关类似问题的问题中找到答案。
protected void Button2_Click(object sender,EventArgs e)
{
string x = TextBox1.Text;
string y = TextBox2.Text;
DevyoWebAapp.localhost.WebService.verification(x,y);
}
[WebMethod]
public string verification(string x, string y)
{
// Create the XmlSchemaSet class.
XmlSchemaSet sc = new XmlSchemaSet();
// Add the schema to the collection before performing validation
sc.Add(x, y);
// Define the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc; // Association
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
// Create the XmlReader object.
XmlReader reader = XmlReader.Create(x, settings);
// Parse the file.
while (reader.Read()) ; // will call event handler if invalid
return ("The XML file validation has completed");
}
// Display any validation errors.
private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
Console.WriteLine("Validation Error: {0}", e.Message);
}
}
答案 0 :(得分:0)
我认为它指的是服务,试试这样:
protected void Button2_Click(object sender, EventArgs e)
{
string x = TextBox1.Text;
string y = TextBox2.Text;
var service = new DevyoWebAapp.localhost.WebService();
service.verification(x, y);
}