怎么将xml文件转存为txt文件 C#

ooyyi6 2014-03-06 10:37:20
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
string strPath = openFileDialog1.FileName;
XmlDocument doc = new XmlDocument();
doc.Load(strPath);

string pathtxt = "dataset.txt";
StreamWriter sw = new StreamWriter(pathtxt);
XmlNodeList nodeList = doc.DocumentElement.ChildNodes;

for (int i = 0; i < nodeList.Count; i++)
{
string Id = nodeList[i].ChildNodes[0].InnerText;
string PostTypeId = nodeList[i].ChildNodes[1].InnerText;
string ParentID = nodeList[i].ChildNodes[2].InnerText;
string AcceptedAnswerId = nodeList[i].ChildNodes[3].InnerText;
string CreationDate = nodeList[i].ChildNodes[4].InnerText;
string Score = nodeList[i].ChildNodes[5].InnerText;
string ViewCount = nodeList[i].ChildNodes[6].InnerText;
string Body = nodeList[i].ChildNodes[7].InnerText;
string OwnerUserId = nodeList[i].ChildNodes[8].InnerText;
string LastEditorUserId = nodeList[i].ChildNodes[9].InnerText;
string LastEditorDisplayName = nodeList[i].ChildNodes[10].InnerText;
string LastEditDate = nodeList[i].ChildNodes[10].InnerText;
string LastActivityDate = nodeList[i].ChildNodes[11].InnerText;
string CommunityOwnedDate = nodeList[i].ChildNodes[12].InnerText;
string ClosedDate = nodeList[i].ChildNodes[13].InnerText;
string Title = nodeList[i].ChildNodes[14].InnerText;
string Tags = nodeList[i].ChildNodes[15].InnerText;
string AnswerCount = nodeList[i].ChildNodes[16].InnerText;
string CommentCount = nodeList[i].ChildNodes[17].InnerText;
string FavoriteCount = nodeList[i].ChildNodes[18].InnerText;

sw.WriteLine(Id+ " " + PostTypeId + " " + ParentID + " " + AcceptedAnswerId + " " + CreationDate + " " +
Score + " " + ViewCount + " " + Body + " " + OwnerUserId + " " + LastEditorUserId + " " + LastEditorDisplayName +
" " + LastEditDate + " " + LastActivityDate + " " + CommunityOwnedDate + " " + ClosedDate + " " + Title +
" " + Tags + " " + AnswerCount + " " + CommentCount + " " + FavoriteCount);
}
sw.Close();

我想将xml文件里的东西转存成txt,但是他一直提示那个nodelist.childnode.innertext没有实例化的对象,求帮助啊~谢谢各位大神
...全文
558 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
ooyyi6 2014-03-06
  • 打赏
  • 举报
回复
引用 3 楼 justsoso66 的回复:
先看看加载进来的xml字符串结构有没有问题,你赋值的时候至少得判断一下nodelist[x].childnodes[i]是不是Null啊,如果都没子节点,你这么取肯定抛异常


那个数据的格式是这样的
justsoso66 2014-03-06
  • 打赏
  • 举报
回复
先看看加载进来的xml字符串结构有没有问题,你赋值的时候至少得判断一下nodelist[x].childnodes[i]是不是Null啊,如果都没子节点,你这么取肯定抛异常
ooyyi6 2014-03-06
  • 打赏
  • 举报
回复
引用 1 楼 justsoso66 的回复:
调试的时候打个断点看看nodelist.childnode是不是NULL
是NULL,所以我也很凌乱不知道为什么那个是NULL
justsoso66 2014-03-06
  • 打赏
  • 举报
回复
调试的时候打个断点看看nodelist.childnode是不是NULL
敌敌畏耶 2014-03-06
  • 打赏
  • 举报
回复
引用 5 楼 zl373946618289 的回复:
我觉得这个问题不那么复杂把?直接操作文件把XML后缀改成.txt不行吗?
zl289kkk 2014-03-06
  • 打赏
  • 举报
回复
我觉得这个问题不那么复杂把?直接操作文件把XML后缀改成.txt不行吗?

111,098

社区成员

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

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

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