大侠们请指点!XML的写入问题!

leoliuy6675 2003-08-12 01:41:38
以下是一个xml文件:
Display the modified XML...
<?xml version="1.0"?>
<!-- Sample XML document -->
<bookstore xmlns:bk="urn:samples">
<book genre="novel" publicationdate="1997" bk:ISBN="1-861001-57-5">
<title>Pride And Prejudice</title>
<author>
<first-name>Jane</first-name>
<last-name>Austen</last-name>
</author>
<price>24.95</price>
</book>
<book genre="novel" publicationdate="1992" bk:ISBN="1-861002-30-1">
<title>The Handmaid's Tale</title>
<author>
<first-name>Margaret</first-name>
<last-name>Atwood</last-name>
</author>
<price>29.95</price>
</book>
</bookstore>
问题:
1:我采用一下方式加入一个节点:
XmlNode currNode = doc.DocumentElement;
XmlElement newElem=doc.CreateElement( "style");
newElem.SetAttribute("style","hardcover");
currNode.FirstChild.AppendChild(newElem);
currNode.LastChild.AppendChild(newElem);
为什么只加入到第二个子级中,第一个子级中没有这个相应的元素?
也就是currNode.FirstChild.AppendChild(newElem);失去了作用
2:如果我想加到第二个子级中,可以不通过currNode.LastClild.AppendChild的方式吗?及如果我有N个子级,我想随便加到任何一个子级中,采用什么方法?
3:如果我要删除其中一个节点,采用什么方法好?
4:如果要修改一个节点那?用什么方法?

谢谢各位大侠指点
...全文
26 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
leoliuy6675 2003-08-17
  • 打赏
  • 举报
回复
谢谢各位捧场!散分了!
bethrezen 2003-08-14
  • 打赏
  • 举报
回复
up
colin666 2003-08-14
  • 打赏
  • 举报
回复
public class XmlConfig
{
private XmlDocument doc;
private string xmlFileName;
private MemoryStream xmlStream;
private byte[] xmlcontent = new byte[0];
public byte[] Content
{
get
{
// xmlcontent = new Byte[xmlStream.Length];
// bool a = xmlStream.CanRead;
// byte[] buffer = xmlStream.GetBuffer();
// System.Array.Copy(buffer,0,xmlcontent,0,xmlcontent.Length);
// return xmlcontent;

}
set
{
}
}
public XmlConfig(string filename)
{

xmlFileName = filename;
doc = new XmlDocument();
try
{
doc.Load(xmlFileName);
}
catch
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}

public XmlConfig(byte[] contain)
{
xmlStream = new MemoryStream();

xmlStream.Write(contain,0,contain.Length);
// MemoryStream stream =new MemoryStream(contain);
// xmlStream = (Stream) stream;
doc = new XmlDocument();

try
{
if(contain.Length==0)
doc.Load(xmlStream);
else
{
string xml = System.Text.Encoding.ASCII.GetString(contain);
doc.LoadXml(xml);
}
}
catch(Exception ee)
{
doc.LoadXml("<?xml version=\"1.0\" encoding=\"gb2312\"?><Settings></Settings>");
}
}
public void Save()
{
try
{
doc.Save(xmlFileName);
}
catch
{
}
}
public void StreamSave()
{
try
{
doc.Save(xmlStream);
}
catch(Exception ee)
{
Console.WriteLine(ee.ToString());

}

}

public string Read(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
return node.InnerText;
else
return value;
}

public void Write(string key, string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
node.InnerText = value;
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
node.InnerText = value;
}
}

//增加一个父节点
public void Write(string key)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if (node != null)
{
}
else
{
node = doc.DocumentElement;
string[] path = key.Split(new char[] {'/'});
for (int i = 0; i < path.Length; i++)
{
XmlNode node2;
if ( (node2 = node.SelectSingleNode(path[i])) == null)
{
node2 = doc.CreateElement(path[i]);
node.AppendChild(node2);
}
node = node2;
}
}

}
public void ChildWrite(string key,string Childkey,string value)
{
XmlNode node = doc.DocumentElement.SelectSingleNode(key);
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
node2.InnerText=value;
}
else
{
XmlNode node3=doc.CreateElement(Childkey);

node.PrependChild(node3);
node3.InnerText=value;

}
}
//key为父节点的值,chlidkey为子节点的值。
public string Read(string key,string Childkey,string value)
{

XmlNode node = doc.DocumentElement.SelectSingleNode(key);
if(node!=null)
{
XmlNode node2=node.SelectSingleNode(Childkey);
if(node2!=null)
{
return node2.InnerText;
}
else
return value;
}
else
return value;
}

public XmlNodeList NodeRead()
{
XmlNode node = doc.ChildNodes[1];
XmlNodeList nodelist = node.ChildNodes;
return nodelist;
}
}
saucer 2003-08-13
  • 打赏
  • 举报
回复
1. you cannot just add the same instance, you need to create a new node:

currNode.FirstChild.AppendChild(newElem);
currNode.LastChild.AppendChild(newElem.CloneNode(true));

>>>我要移去<last-name>Atwood</last-name>,采用什么方法呀

XmlNode node = doc.SelectSingleNode("//last-name[.='Atwood']");
if (node != null)
node.ParentNode.RemoveChild(node);
leoliuy6675 2003-08-13
  • 打赏
  • 举报
回复
例如:我要移去<last-name>Atwood</last-name>,采用什么方法呀?
用RemoveAll的方法,好像只能清除Atwood,不能清除<last-name></last-name>
真的不懂,弄了很长时间了!郁闷之极!
lemong 2003-08-12
  • 打赏
  • 举报
回复
1。不太清楚

2.用这样的方法可以访问
XmlNode currNode = doc.DocumentElement;
currNode.ChildNodes[i].AppendChild

3,4。其他相关方法

PrependChild 将指定的节点添加到该节点子级列表的开头。
RemoveAll 移除当前节点的所有子级和/或属性。
RemoveChild 移除指定的子节点。
ReplaceChild 用 newChild 节点替换子节点 oldChild。

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧