还是问XmlTextReader和XmlTextWriter一起实现对xml文件的更新、修改、删除操作(代码)

wintle 2003-10-30 09:32:43
之前已经问过一次这个问题,在大家的帮助下,终于有了一点起色,但还没有能最终解决问题,现将xml和代码都贴上来,希望能得到高手帮忙。。
XML:
FoverCode.xml:

<?xml version="1.0" encoding="GB2312"?>
<FavorCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FavorCode.xsd">
<GeneralSettings>
<RecentID>0</RecentID>
<LastUpdate>1967-08-15</LastUpdate>
<EnableAutoUpdate>1</EnableAutoUpdate>
<ShareSum>0</ShareSum>
</GeneralSettings>
<CodeCatag ID="1" Description="SqlServer">
<Code ID="1" Description="StringA">
<Article ID="1">
<Name>StringA</Name>
<Author>String</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail><![CDATA[sdersdfsdfsdfsdfsdfsf]]></Detail>
<Remark>StringA</Remark>
<Level>0</Level>
</Article>
<Article ID="2">
<Name>Stringd</Name>
<Author>String2</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail>String2</Detail>
<Remark>String2</Remark>
<Level>0</Level>
</Article>
</Code>
<Code ID="2" Description="StringB">
<Article ID="3">
<Name>Stringe</Name>
<Author>String</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail>String3</Detail>
<Remark>String3</Remark>
<Level>0</Level>
</Article>
<Article ID="4">
<Name>Stringf</Name>
<Author>String</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail>String4</Detail>
<Remark>String4</Remark>
<Level>0</Level>
</Article>
</Code>
</CodeCatag>
<CodeCatag ID="2" Description="C#">
<Code ID="3" Description="正则表达式">
<Article ID="5">
<Name>身份证</Name>
<Author>Wintle</Author>
<StoreDate>2003-10-27</StoreDate>
<Detail>\d{17}(\d|[xX]){1}|\d{15}</Detail>
<Remark>解决了最后一位是xX的问题</Remark>
<Level>3</Level>
</Article>
<Article ID="6">
<Name>EMail</Name>
<Author>Wintle</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail>\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*</Detail>
<Remark>一般</Remark>
<Level>0</Level>
</Article>
</Code>
<Code ID="4" Description="DataGrid">
<Article ID="7">
<Name>String</Name>
<Author>String</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail>String</Detail>
<Remark>String</Remark>
<Level>0</Level>
</Article>
<Article ID="8">
<Name>String</Name>
<Author>String</Author>
<StoreDate>1967-08-13</StoreDate>
<Detail>String</Detail>
<Remark>String</Remark>
<Level>0</Level>
</Article>
</Code>
</CodeCatag>
</FavorCode>

代码(希望实现更新Article下的内容,通过Article的ID属性):

private void btnReadXml_Click(object sender, System.EventArgs e)
{
if(System.IO.File.Exists(Application.StartupPath+"\\~FavorCode.xml"))
{
XmlTextReader reader = new XmlTextReader(Application.StartupPath+"\\~FavorCode.xml");
XmlTextWriter write = new XmlTextWriter(Application.StartupPath+"\\~~FavorCode.xml",System.Text.Encoding.GetEncoding("GB2312"));
write.Formatting=Formatting.Indented;
reader.MoveToContent(); reader.WhitespaceHandling=WhitespaceHandling.None;
write.WriteStartDocument();
write.WriteStartElement(reader.Name);
write.WriteAttributes(reader,true);
while(reader.Read())
{
if(reader.NodeType==XmlNodeType.Element)
{
switch(reader.LocalName)
{
case "GeneralSettings":
write.WriteNode(reader,true);
break;
case "CodeCatag": ///(A处)
write.WriteStartElement("CodeCatag");
write.WriteAttributes(reader,true);
break;
case "Code":
write.WriteStartElement("Code");
write.WriteAttributes(reader,true);
break;
case "Article":
if(reader.GetAttribute("ID").Trim().Equals(this.txtArticleID.Text.Trim()))
{ write.WriteStartElement("Article"); write.WriteAttributeString("ID",reader.GetAttribute("ID"));
write.WriteElementString("Name",this.txtName.Text);
write.WriteElementString("Author",this.txtAuthor.Text);
write.WriteElementString("StoreDate",this.txtStoreDate.Text);
write.WriteStartElement("Detail");
write.WriteCData(this.rtxtXml.Text);
write.WriteEndElement();
write.WriteStartElement("Remark");
write.WriteCData(this.rtxtXml.Text);
write.WriteEndElement();
write.WriteElementString("Level",reader.GetAttribute("ID"));
write.WriteEndElement();
}
else { write.WriteNode(reader,true);
}
break;
}
}
else if(reader.NodeType==XmlNodeType.EndElement)
{
switch(reader.LocalName)
{ case "CodeCatag": ///(B处)
write.WriteEndElement(); break;
case "Code":
write.WriteEndElement();
break;
}
}
}
write.WriteEndElement();
write.WriteEndDocument();
write.Flush();
write.Close();
reader.Close();
}
}

基本思路是从原文件中读出来,然后写到一个新的xml里去。但是出现了问题:
1、如果B处存在,就报错。
2、如果把B处去掉,则在A处出一个问题,即当写到新的~~FavorCode.xml中时,第一个<CodeCatag..>不存在。

请高手见教!!!

由于FavorCode.xml可能会很大,所以只能采用xmltextreader和writer的方式吧,但我不知道我理解的对不对,这样处理?但我实在想不到什么更好的方式了。我是想如果可以直接不用这个倒来倒去就好了,而是直接在FavorCode.xml中操作。
...全文
202 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
wintle 2003-11-01
  • 打赏
  • 举报
回复
过奖了,分数送给你:)
cgsun 2003-10-30
  • 打赏
  • 举报
回复
不笨,不笨.想想郭大俠......
wintle 2003-10-30
  • 打赏
  • 举报
回复
自已解决了!

private void btnReadXml_Click(object sender, System.EventArgs e)
{
if(System.IO.File.Exists(Application.StartupPath+"\\~FavorCode.xml"))
{
XmlTextReader reader = new XmlTextReader(Application.StartupPath+"\\~FavorCode.xml");
XmlTextWriter write = new XmlTextWriter(Application.StartupPath+"\\~~FavorCode.xml",System.Text.Encoding.GetEncoding("GB2312"));
write.Formatting=Formatting.Indented;
reader.MoveToContent();
reader.WhitespaceHandling=WhitespaceHandling.None;
write.WriteStartDocument();
write.WriteStartElement(reader.Name);
write.WriteAttributes(reader,true);
reader.Read();
while(!reader.EOF)
{
if(reader.NodeType==XmlNodeType.Element)
{
switch(reader.LocalName)
{
case "GeneralSettings":
write.WriteNode(reader,true);
break;
case "CodeCatag":
write.WriteStartElement("CodeCatag");
write.WriteAttributes(reader,true);
reader.Read();
break;
case "Code":
write.WriteStartElement("Code");
write.WriteAttributes(reader,true);
reader.Read();
break;
case "Article":
if(reader.GetAttribute("ID").Trim().Equals(this.txtArticleID.Text.Trim()))
{
write.WriteStartElement("Article");
write.WriteAttributeString("ID",reader.GetAttribute("ID"));
write.WriteElementString("Name",this.txtName.Text);
write.WriteElementString("Author",this.txtAuthor.Text);
write.WriteElementString("StoreDate",this.txtStoreDate.Text);
write.WriteStartElement("Detail");
write.WriteCData(this.rtxtXml.Text);
write.WriteEndElement();
write.WriteStartElement("Remark");
write.WriteCData(this.rtxtXml.Text);
write.WriteEndElement();
write.WriteElementString("Level",reader.GetAttribute("ID"));
write.WriteEndElement();
reader.Read();
}
else
{
write.WriteNode(reader,true);
}
break;
default:
reader.Read();
break;
}
}
else if(reader.NodeType==XmlNodeType.EndElement)
{
switch(reader.LocalName)
{
case "CodeCatag":
write.WriteEndElement();
reader.Read();
break;
case "Code":
write.WriteEndElement();
reader.Read();
break;
default:
reader.Read();
break;
}
}
else
{
reader.Read();
}
}
write.WriteEndElement();
write.WriteEndDocument();
write.Flush();
write.Close();
reader.Close();
}
}


关键点:write.WriteNode(reader,true);这句中相当于执行了一次reader.Read();

一个星期的努力!

我笨吧:)
C# XML入门经典——C#编程人员必备的XML技能 作者:[美]Stewart Fraser, Steven 著,毛尧飞,崔伟 译 出版社:清华大学出版社 出版时间:2003年11月 第1章 在C#中使用XML的原因1.1 使用XML的原因1.1.1 开放性1.1.2 简单性1.1.3 自我描述性1.1.4 互操作性1.1.5 结构1.1.6 分开结构和内容1.1.7 可扩展性1.2 什么是XML1.2.1 XML涉及多种语言1.2.2 XML文档1.3 使用XML的对象1.3.1 内容表示1.3.2 B2B电子商务1.3.3 远程过程调用1.3.4 数据存储和访1.3.5 不使用XML的情况1.4 XML标准1.4.1 什么是W3C1.4.2 XML标准1.4.3 与XML相关的标准1.4.4 标准重要的原因1.5 XML如何适应.NET1.5.1 在.NET Framework中使用XML1.5.2 .NET中的XML支持1.6 小结第2章 XML概述2.1 XML的概念2.1.1 XML元素2.1.2 XML属性2.1.3 XML解析器2.1.4 构建XML2.1.5 XML文档的各个组成部分2.2 创建格式良好的XML文档2.2.1 XML中的元素2.2.2 XML中的属性2.2.3 在XML中使用注释2.3 验证XML文档的有效性2.3.1 文档类型定义2.3.2 XML Schema2.3.3 XML编码2.4 小结第3章 在.NET中使用XML3.1 XML如何适合.NET3.1.1 XML3.1.2 文档对象模型(DOM)3.1.3 命名空间3.1.4 DTD和XML Schema3.1.5 XPath 3.1.6 XSLT3.2 .NET Framework使用XML3.2.1 配置文件3.2.2 ADO.NET3.2.3 SOAP和Web服务3.3 案例分析——电话簿样式应用程序3.4 小结第4章 在.NET中读取XML4.1 流模型4.1.1 流模型和DOM的比较4.1.2 流模型中的变体4.2 XmlTextReader类4.2.1 XmlTextReader属性4.2.2 读取属性4.2.3 读取较大的数据块4.3 XmlNodeReader类4.4 XmlValidatingReader类4.5 小结第5章 在.NET中编写XML5.1 利用.NET类编写XML文档5.2 XmlWriter类5.2.1 XmlWriter方法5.2.2 XmlWriter属性5.3 XmlWriter类5.3.1 XmlTextWriter构造函数5.3.2 XmlTextWirter属性5.3.3 处理XmlTextWirter5.3.4 写入较大的数据块5.4 小结第6章 在.NET中实现DOM6.1 文档对象模型6.1.1 文档对象模型与流模型6.1.2 .NET DOM继承模型6.2 XmlNode类6.2.1 XmlNode的定义6.2.2 XmlNode的属性6.2.3 XmlNode的方法6.3 XmlDocument类6.3.1 创建节点6.3.2 加载和保存6.3.3 迭代XmlDocument实例6.3.4 编辑XML文档6.4 案例分析6.4.1 体系结构6.4.2 应用程序详细信息6.4.3 加载用户联系人6.4.4 搜索联系人6.4.5 导出联系人6.4.6 导入其他联系人6.5 小结第7章 XPath和.NET7.1 System.Xml.XPath命名空间7.2 .NET中的XPath类7.2.1 XPathDocument类

110,578

社区成员

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

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

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