XML的简单读取问题,急!,谢谢!

JB-Zhang 2003-08-25 03:16:11
有一个结构如下的XML文件,请问怎么把它读进XMLtextReader里面?谢谢
<xml version="1.0" encoding="GB2312">
<unit name="M" type="长度" value="1" />
<unit name="米" type="长度" value="1" />
<unit name="公尺" type="长度" value="1" />
<unit name="CM" type="长度" value="0.01" />
<unit name="厘米" type="长度" value = "0.01" />
<unit name="公分" type="长度" value = "0.01" />
<unit name="MM" type="长度" value = "0.001" />
<unit name="毫米" type="长度" value = "0.001" />
<unit name="KM" type="长度" value = "1000" />
<unit name="千米" type="长度" value = "1000" />
<unit name="公里" type="长度" value = "1000" />
<unit name="里" type="长度" value = "500" />
...全文
29 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
JB-Zhang 2003-08-25
  • 打赏
  • 举报
回复
它出现了无效编码的错误了,难道XML不支持中文吗?encoding="GB2312"还不对吗?
firejie 2003-08-25
  • 打赏
  • 举报
回复
while(reader.read())
{
if(reader.NodeType==XmlNodeType.XmlDeclaration)
{
richTextBox1.AppendText("<?xml version='1.0' encoding='utf-8'?>"+"\n");
}


if(reader.NodeType==XmlNodeType.Element)
{
richTextBox1.AppendText("<"+reader.Name+">");
reader.Read();
}

if(reader.NodeType==XmlNodeType.Element)
{
richTextBox1.AppendText("\n"+"<"+reader.Name+">");
reader.Read();
}


if(reader.NodeType==XmlNodeType.Element)
richTextBox1.AppendText("\n"+"<"+reader.Name+">");
else if(reader.NodeType==XmlNodeType.Text)
richTextBox1.AppendText(reader.Value);
else if(reader.NodeType==XmlNodeType.EndElement)
richTextBox1.AppendText("</"+reader.Name+">"+"\n");

}
JB-Zhang 2003-08-25
  • 打赏
  • 举报
回复
To HapTears(玻璃*杯) :
我把文件改成合法了,也用XmlTextReader读出来 了,可是就是不知道怎么从reader对象里面读出一个值业
cnhgj 2003-08-25
  • 打赏
  • 举报
回复
楼上的,不用加</xml>也是有效的xml文件!
HapTears 2003-08-25
  • 打赏
  • 举报
回复
这个不是有效的xml文件少了</xml>!
如果是有效的xml文件
using System;
using System.IO;
using System.Xml;

//Reads an XML document

public class Sample
{
private const String filename = "items.xml";

public static void Main()
{
XmlTextReader reader = null;

try
{
//Load the reader with the data file and ignore all white space nodes.
reader = new XmlTextReader(filename);
}

finally
{
if (reader!=null)
reader.Close();
}
}

}
雪狼1234567 2003-08-25
  • 打赏
  • 举报
回复
using System;
using System.Data.SqlClient;
using System.Data;


class TestMulti
{
public static void Main()
{
string sFileName = "MyTest.xml";

SqlDataAdapter da = new SqlDataAdapter("select * from authors; select * from titles; select * from sales",
"server=localhost;database=pubs;uid=sa;pwd=;");

da.TableMappings.Add("Table","authors");
da.TableMappings.Add("Table1","titles");
da.TableMappings.Add("Table2","sales");

DataSet ds = new DataSet();
da.Fill(ds);

ds.WriteXml(sFileName, XmlWriteMode.WriteSchema);

ds.Dispose();
ds = null;
ds = new DataSet();
ds.ReadXml(sFileName);
foreach (DataTable dt in ds.Tables)
{
Console.WriteLine("{0}, rowcount:{1}", dt.TableName, dt.Rows.Count);
}

}
}

雪狼1234567 2003-08-25
  • 打赏
  • 举报
回复
DataSet ds=new DataSet("XMLProducts");
//连接SQL Server数据库
SqlConnection conn=new SqlConnection(@"server=glf;uid=sa;pwd=;database=northwind");
SqlDataAdapter da=new SqlDataAdapter("SELECT * FROM products",conn);
//绑定DataGrid控件
da.Fill(ds,"products");
dataGrid1.DataSource=ds;
dataGrid1.DataMember="products";
doc=new XmlDataDocument(ds);
XmlNodeList nodeLst=doc.GetElementsByTagName("ProductName");
//将Xml结点加入item中
foreach(XmlNode nd in nodeLst)
listBox1.Items.Add(nd.InnerText);
//存盘
string file="product.xml";
ds.WriteXml(file);

110,534

社区成员

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

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

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