📄 productcatalog4439.cs
字号:
using System;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace CSProduct_Catalog4439
{
public partial class ProductCatalog4439 : Form
{
private XmlDataDocument xml_data;
private DataSet relational_data;
public ProductCatalog4439()
{
InitializeComponent();
}
private void DataGrid1_CurrentCellChanged(object sender, EventArgs e)
{
Xml_TxtBx.Text = xml_data.InnerXml.ToString();
}
private void Button1_Click(object sender, EventArgs e)
{
if (Button1.Text.Equals("Query"))
{
if (RadioButton1.Checked)
{
// Create an XPathNavigator object
XPathNavigator navigator;
navigator = xml_data.CreateNavigator();
// Create and compile an XPathExpression
XPathExpression expr;
try
{
expr = navigator.Compile(
"descendant::product[id=" +
Id_TxtBx.Text + "]");
// Call the Select method on the XPathNavigator
// object and store the reference to the
// XPathNodeIterator object returned by the
// Select method
XPathNodeIterator iterator;
iterator = navigator.Select(expr);
// Navigate to get the value of id, name,
// and price
iterator.MoveNext();
XPathNavigator nav2 = iterator.Current.Clone();
nav2.MoveToFirstChild();
Id_TxtBx.Text = nav2.Value;
nav2.MoveToNext();
Name_TxtBx.Text = nav2.Value;
nav2.MoveToNext();
Price_TxtBx.Text = nav2.Value;
}
catch (Exception ex)
{
MessageBox.Show("Enter a valid product id");
}
}
else if (RadioButton2.Checked)
{
// Create an XPathNavigator object
XPathNavigator navigator;
navigator = xml_data.CreateNavigator();
// Create and compile an XPathExpression
XPathExpression expr;
try
{
expr = navigator.Compile(
"descendant::product[name='" +
Name_TxtBx.Text + "']");
// Call the Select method on the XPathNavigator
// object and store the reference to the
// XPathNodeIterator object returned by the
// Select method
XPathNodeIterator iterator;
iterator = navigator.Select(expr);
// Navigate to get the value of id, name,
// and price
iterator.MoveNext();
XPathNavigator nav2 = iterator.Current.Clone();
nav2.MoveToFirstChild();
Id_TxtBx.Text = nav2.Value;
nav2.MoveToNext();
Name_TxtBx.Text = nav2.Value;
nav2.MoveToNext();
Price_TxtBx.Text = nav2.Value;
}
catch (Exception ex)
{
MessageBox.Show("Enter a valid product name, " +
"which is case sensitive.");
}
}
Button1.Text = "Clear";
GroupBox1.Text = "Clear Query";
}
else
{
Id_TxtBx.Text = "";
Name_TxtBx.Text = "";
Price_TxtBx.Text = "";
Button1.Text = "Query";
GroupBox1.Text = "Enter Query";
}
}
private void RadioButton2_CheckedChanged(object sender, EventArgs e)
{
Id_TxtBx.ReadOnly = true;
Name_TxtBx.ReadOnly = false;
Name_TxtBx.Focus();
}
private void RadioButton1_CheckedChanged(object sender, EventArgs e)
{
Id_TxtBx.ReadOnly = false;
Name_TxtBx.ReadOnly = true;
Id_TxtBx.Focus();
}
private void Button2_Click(object sender, EventArgs e)
{
// Read the XML data from the Xml_TxtBx into the DataSet
// object
XmlTextReader xml_reader = new XmlTextReader(new
StringReader(Xml_TxtBx.Text));
relational_data = new DataSet();
relational_data.ReadXml(xml_reader);
// Create a new XmlDataDocument object
xml_data = new XmlDataDocument(relational_data);
// Save the changes in the products.xml file
relational_data.WriteXml(@"..\..\products4439.xml");
// Show the data in the DataGrid
DataGrid1.DataSource = xml_data.DataSet.Tables[0];
}
private void ProductCatalog_Load(object sender, EventArgs e)
{
// Load products data into the DataSet object
relational_data = new DataSet();
relational_data.ReadXml(@"..\..\products4439.xml");
// Display the data in DataGrid1
DataGrid1.DataSource = relational_data.Tables[0];
// Synchronize the DataSet with the XmlDataDocument object.
// Any change made in the DataSet object is automatically
// made in the XmlDataDocument object.
xml_data = new XmlDataDocument(relational_data);
// Show the XML view in the Xml_TxtBx textbox
Xml_TxtBx.Text = xml_data.InnerXml.ToString();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -