c#从xml文件中读取 [英] c# read from xml file
问题描述
您好,
我有和xml文件,如下所示:
Hello,
I have and xml file, like this:
<MyXMLFile>
<SerialNumber>111</SerialNumber>
<Name>MyName</Name>
<Size X="10" Y="12" />
</MyXMLFile>
创建XML数据文件的用户可以更改元素的顺序。
例如,size元素可以在name元素之前。
我必须读取此文件的全部数据。
据我所知,XMLReader不能退出它的位置。
所以我想到的一个解决方案是创建一个xml阅读器并销毁它,一次又一次。
例如:
The user who creates the XML data file, can change the order of the elements.
For example, "size" element can be before "name" element.
I have to read the whole data of this file.
As far as I know, XMLReader can't go back from it's position.
So one solution I thought about, is to create an xml reader and destroy it, again and again.
For example:
XmlReader xmlReader = XmlReader.Create(strXMLFileName);
xmlReader.ReadToFollowing(XMLNODE_Size);
int sizeX = Convert.ToInt32(xmlReader.GetAttribute("X"));
int sizeY= Convert.ToInt32(xmlReader.GetAttribute("Y"));
xmlReader.Close();
xmlReader = XmlReader.Create(strXMLFileName);
xmlReader.ReadToFollowing(XMLNODE_NAME);
xmlReader.ReadStartElement();
string name = xmlReader.ReadString();
xmlReader.ReadEndElement();
xmlReader.Close();
有更好的方法吗,而不是关闭并重新打开xmlReader?
(我也想阅读有的地方。)
谢谢
Is there a better way to do it, instead of close and reopen the xmlReader?
(I want also to read the attibutes where there are.)
Thanks
推荐答案
试试使用XmlDocument。
它是这样的
Try using XmlDocument.
It goes something like this
XmlDocument xDoc = new XmlDocument();
xDoc.Load("YourFile.Xml");
string sizeValue = xDoc.DocumentElement.SelectSingleNode("Size").InnerText;
string nameValue = xDoc.DocumentElement.SelectSingleNode("Name").InnerText;
string serialNumbersizeValue = xDoc.DocumentElement.SelectSingleNode("SerialNumber").InnerText;
希望有所帮助。如果确实将答案标记为解决方案/ upvote。
谢谢
Milind
Hope that helps. If it does mark the answer as solution/upvote.
Thanks
Milind
//读取XML文件的方法感觉不错
下拉列表
//Method to read XML File and feel out
dropdownlist
public void GetAllLocations()
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("~\\App_Data\\Location.xml"));
try
{
if (ds.Tables[0].Rows.Count > 0)
{
DDLLocation.Enabled = true;
DDLLocation.DataSource = ds;
DDLLocation.DataTextField = "LocationName";
DDLLocation.DataValueField = "LocationName";
DDLLocation.DataBind();
DDLLocation.Items.Insert(0, new ListItem("(Please Select)", Convert.ToString(0))); // Add as first item
}
else
{
DDLLocation.Items.Insert(0, new ListItem("Location not found", Convert.ToString(0)));
return;
}
}
catch (Exception ex)
{
lblMessage.Text = "Load Location... " + ex.Message.ToString();
return;
}
}
// XML DATA
//XML DATA
<mydata>
<admin>
<username>
admin
</username>
<password>
admin1111
</password>
</admin>
<admin>
<username>
admin1234
</username>
<password>
1234
</password>
</admin>
</mydata>
//读取XML文件的方法
//Method to read XML File
XmlDocument xDoc = new XmlDocument();
xDoc.Load("login.xml"); //XML File name (if winforms keep the XML file in bin directory)
//Case 1: When only one value in xml File
string username = xDoc.SelectSingleNode("mydata/admin/username").InnerText.Trim();
string password = xDoc.SelectSingleNode("mydata/admin/password").InnerText.Trim();
//Case 1 Ends
//Case 2: when more than one value in xml File (looping)
var mydata = xDoc.SelectNodes("mydata");
for (int i = 0; i < mydata.Count; i++)
{
foreach (XmlNode node in mydata[i].ChildNodes)
{
string uname = node.SelectSingleNode("username").InnerText.Trim();
string pwd = node.SelectSingleNode("password").InnerText.Trim();
}
}
//案例2结束
//Case 2 Ends
这篇关于c#从xml文件中读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!