Sabe-se que existe um arquivo XML (bookstore.xml) da seguinte forma:
<?xml version="1.0" encoding="gb2312"?>
<livraria>
<gênero do livro="fantasia" ISBN="2-3631-4">
<title>O Legado de Oberon</title>
<autor>Corets, Eva</autor>
<preço>5,95</preço>
</livro>
</bookstore>
1. Insira um nó <book> no nó <bookstore>:
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load("livraria.xml");
XmlNode root=xmlDoc.SelectSingleNode("livraria");//Encontrar <livraria>
XmlElement xe1=xmlDoc.CreateElement("book");//Cria um nó <book>
xe1.SetAttribute("genre","Li Zanhong");//Define o atributo de gênero do nó
xe1.SetAttribute("ISBN","2-3631-4");//Definir o atributo ISBN do nó
XmlElement xesub1=xmlDoc.CreateElement("title");
xesub1.InnerText="CS do iniciante ao proficiente";//Definir nós de texto
xe1.AppendChild(xesub1);//Adicionar ao nó <book>
XmlElement xesub2=xmlDoc.CreateElement("autor");
xesub2.InnerText="houjie";
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("preço");
xesub3.InnerText="58.3";
xe1.AppendChild(xesub3);
root.AppendChild(xe1);//Adicionar ao nó <bookstore>
xmlDoc.Save("livraria.xml");
//==============================================
O resultado é:
<?xml version="1.0" encoding="gb2312"?>
<livraria>
<gênero do livro="fantasia" ISBN="2-3631-4">
<title>O Legado de Oberon</title>
<autor>Corets, Eva</autor>
<preço>5,95</preço>
</livro>
<gênero do livro="李zanhong" ISBN="2-3631-4">
<title>CS da entrada ao mestre</title>
<autor>Houjie</autor>
<preço>58,3</preço>
</livro>
</bookstore>
2. Modifique o nó: altere o valor do gênero do nó cujo valor do atributo de gênero é "Li Zanhong" para "atualizar Li Zanhong" e modifique o texto do nó filho <autor> deste nó para "Ya Sheng".
XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//Obtém todos os nós filhos do nó da livraria
foreach(XmlNode xn em nodeList) // Percorre todos os nós filhos
{
XmlElement xe=(XmlElement)xn;//Converte o tipo de subnó para o tipo XmlElement
if(xe.GetAttribute("genre")=="Li Zanhong")//Se o valor do atributo de gênero for "Li Zanhong"
{
xe.SetAttribute("genre","update Li Zanhong");//Modifique este atributo para "update Li Zanhong"
XmlNodeList nls=xe.ChildNodes;//Continue para obter todos os nós filhos do nó filho xe
foreach(XmlNode xn1 em nls)//Traverse
{
XmlElement xe2=(XmlElement)xn1;//Tipo de conversão
if(xe2.Name=="autor")//Se encontrado
{
xe2.InnerText="Yasheng";//Modificar
break; //Basta encontrá-lo e sair.
}
}
quebrar;
}
}
xmlDoc.Save("livraria.xml");//Salvar.
//============================================== ==
O resultado final é:
<?xml version="1.0" encoding="gb2312"?>
<livraria>
<gênero do livro="fantasia" ISBN="2-3631-4">
<title>O Legado de Oberon</title>
<autor>Corets, Eva</autor>
<preço>5,95</preço>
</livro>
<gênero do livro="atualização李zanhong" ISBN="2-3631-4">
<title>CS da entrada ao mestre</title>
<autor>Assalto</autor>
<preço>58,3</preço>
</livro>
</bookstore>
3. Exclua o atributo de gênero do nó <book Gener="fantasy" ISBN="2-3631-4"> e exclua o <book Gener="update Li Zanhong" ISBN="2-3631-4 "> nó.
XmlNodeList xnl=xmlDoc.SelectSingleNode("livraria").ChildNodes
(XmlNode xn em xnl)
{
XmlElement xe=(XmlElement)xn;
if(xe.GetAttribute("gênero")=="fantasia")
{
xe.RemoveAttribute("genre");//Exclui o atributo de gênero
}
senão if(xe.GetAttribute("gênero")=="atualizarLi Zanhong")
{
xe.RemoveAll();//Exclui todo o conteúdo do nó
}
}
xmlDoc.Save("livraria.xml");
//===========================================
O resultado final é:
<?xml version="1.0" encoding="gb2312"?>
<livraria>
<livro ISBN="2-3631-4">
<title>O Legado de Oberon</title>
<autor>Corets, Eva</autor>
<preço>5,95</preço>
</livro>
<livro>
</livro>
</bookstore>
4. Exiba todos os dados.
xn
=xmlDoc.SelectSingleNode("livraria")
;
{
XmlElement xe=(XmlElement)xnf;
Console.WriteLine(xe.GetAttribute("genre"));//Exibir valor do atributo
Console.WriteLine(xe.GetAttribute("ISBN"));
XmlNodeList xnf1=xe.ChildNodes;
foreach (XmlNode xn2 em xnf1)
{
Console.WriteLine(xn2.InnerText); //Exibe o texto do nó filho
}
}