正在研究一个简单的XML Web服务,它允许您对XML文件执行简单的CRUD操作。这是我的客户代码:
@WebMethod(operationName = "Delete")
public boolean Delete(String std_id) throws Exception {
File file = new File("C:/Users/user/Desktop/myStudentInstance/myStudentInstance.xml");
boolean isDeleted = true;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
org.w3c.dom.Document doc = db.parse(file);
NodeList root = doc.getElementsByTagName("Students");
NodeList nList2 = doc.getElementsByTagName("student");
for (int i = 0; i < nList2.getLength(); i++) {
Node nNode = root.item(0);
Node nNode2 = nList2.item(i);
Element eElement2 = (Element) nNode2;
if (eElement2.getElementsByTagName("Student_id").item(0).getTextContent().equals(std_id)) {
isDeleted = true;
nNode.removeChild(nNode2);
} else if (eElement2.getElementsByTagName("Student_id").item(0).getTextContent() != (std_id)) {
isDeleted = false;
}
DOMSource source = new DOMSource(doc);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
StreamResult result = new StreamResult(file);
transformer.transform(source, result);
}
System.out.println(isDeleted);
return isDeleted;
}
这是我在Visual Studio上的代码:
private void Delete_Click(object sender, EventArgs e)
{
var student_ID = textBox1.Text.ToString();
Boolean isDeleted = true;
ServiceReference1.StudentWSClient myRef = new ServiceReference1.StudentWSClient();
ServiceReference1.DeleteResponse response = new ServiceReference1.DeleteResponse();
myRef.Delete(student_ID);
if (response.@return == isDeleted )
{
MessageBox.Show("Student Deleted", "Delete Student", MessageBoxButtons.OK);
textBox1.Text = "";
}
else if(response.@return != isDeleted)
{
MessageBox.Show("Student_ID not found or An error has occured!", "Delete Student", MessageBoxButtons.OK);
}
}
我想检查元素是否已被删除,然后显示正确的消息框...当删除时我的返回值为TRUE,但是当它在VS上检查其响应时它显示为false,显示&#34;未找到Student_ID或者发生了错误!&#34; 但元素仍然被删除!有什么原因吗?
答案 0 :(得分:0)
isDeleted = myRef.Delete(student_ID);
您尚未将返回值指定给isDeleted
,请执行上述操作
答案 1 :(得分:0)
每当从列表中删除项目时,总是从头到尾。如果你没有
,索引会搞砸for (int i = nList2.getLength() - 1; i >= 0; i--)