110,566
社区成员
发帖
与我相关
我的任务
分享
<UserControl.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="Resources/StringResource.zh-CN.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</UserControl.Resources>
label1.Content = (string)this.Resources["WinTitle"];
ResourceDictionary languageResDic = new ResourceDictionary();
languageResDic.Source = new Uri(@"Resources/StringResource.zh-CN.xaml", UriKind.RelativeOrAbsolute);
this.Resources.MergedDictionaries.Add(languageResDic);
languageResDic.Source = new Uri(@"D:\Code\DigitalChina\Demo\WinFormToTestWPFControl\WinFormToTestWPFControl\bin\Debug\Resources", UriKind.Absolute);
//创建一个节点
private void buttonCreateNode_Click(object sender, EventArgs e)
{
// Load the XML document
XmlDocument document = new XmlDocument();
document.Load("../../Books.xml");
// Get the root element
XmlElement root = document.DocumentElement;
// Create the new nodes
XmlElement newBook = document.CreateElement("book");
XmlElement newTitle = document.CreateElement("title");
XmlElement newAuthor = document.CreateElement("author");
XmlElement newCode = document.CreateElement("code");
XmlText title = document.CreateTextNode("Beginning Visual C# 3rd Edition");
XmlText author = document.CreateTextNode("Karli Watson et al");
XmlText code = document.CreateTextNode("1234567890");
XmlComment comment = document.CreateComment("This book is the book you are reading");
// Insert the elements
newBook.AppendChild(comment);
newBook.AppendChild(newTitle);
newBook.AppendChild(newAuthor);
newBook.AppendChild(newCode);
newTitle.AppendChild(title);
newAuthor.AppendChild(author);
newCode.AppendChild(code);
root.InsertAfter(newBook, root.LastChild);
document.Save("../../Books.xml");
listBoxXmlNodes.Items.Clear();
RecurseXmlDocument((XmlNode)document.DocumentElement, 0);
}
//删除一个节点
private void buttonDeleteNode_Click(object sender, EventArgs e)
{
// Load the XML document
XmlDocument document = new XmlDocument();
document.Load("../../Books.xml");
// Get the root element
XmlElement root = document.DocumentElement;
// Find the node. root is the < books> tag, so its last child which will be the
// last < book> node
if (root.HasChildNodes)
{
XmlNode book = root.LastChild;
// Delete the child
root.RemoveChild(book);
// Save the document back to disk
document.Save("../../Books.xml");
listBoxXmlNodes.Items.Clear();
RecurseXmlDocument((XmlNode)document.DocumentElement, 0);
}
}
//在一个ListBox中显示文档的所有节点名称以及文本节点的内容
private void RecurseXmlDocument(XmlNode root, int indent)
{
// Make sure we don't do anything if the root is null
if (root == null)
return;
if (root is XmlElement) // Root is an XmlElement type
{
// first, print the name
listBoxXmlNodes.Items.Add(root.Name.PadLeft(root.Name.Length + indent));
// Then check if there are any child nodes and if there are, call this
// method again to print them
if (root.HasChildNodes)
RecurseXmlDocument(root.FirstChild, indent + 2);
// Finally check to see if there are any siblings and if there are
// call this method again to have them printed
if (root.NextSibling != null)
RecurseXmlDocument(root.NextSibling, indent);
}
else if (root is XmlText)
{
// Print the text
string text = ((XmlText)root).Value;
listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));
}
else if (root is XmlComment)
{
// Print text
string text = root.Value;
listBoxXmlNodes.Items.Add(text.PadLeft(text.Length + indent));
// Then check if there are any child nodes and if there are, call this
// method again to print them
if (root.HasChildNodes)
RecurseXmlDocument(root.FirstChild, indent + 2);
// Finally check to see if there are any siblings and if there are
// call this method again to have them printed
if (root.NextSibling != null)
RecurseXmlDocument(root.NextSibling, indent);
}
}
//XPath选择一个节点
//XPath语法相关参考http://www.w3school.com.cn/xpath/xpath_syntax.asp
private void buttonQueryNode_Click(object sender, EventArgs e)
{
// Load the XML document
XmlDocument document = new XmlDocument();
document.Load(@filePath);
// Get the root element
XmlElement root = document.DocumentElement;
string queryStr = textBoxQueryText.Text;
XmlNodeList nodeList = root.SelectNodes(queryStr);
listBoxXmlNodes.Items.Clear();
foreach (XmlNode n in nodeList)
{
RecurseXmlDocument(n, 0);
}
}