
可读取多行内容,仅供参考
1xml文件内容:
<xml version="10" encoding="utf-8">
<filesauto>
<file>
<type>文件</type>
<state>√</state>
<path>C:\Users\mirror\Desktop\备份资料txt</path>
<text>未备份</text>
</file>
<file>
<type>文件</type>
<state>√</state>
<path>C:\Users\mirror\Desktop\待办txt</path>
<text>未备份</text>
</file>
<folder>
<type>文件夹</type>
<state>√</state>
<path>E:\Videos</path>
<text>未备份</text>
</folder>
<folder>
<type>文件夹</type>
<state>√</state>
<path>E:\Pictures</path>
<text>未备份</text>
</folder>
</filesauto>
2新建的类
public class FilesAuto
{
public FilesAuto()
{ }
private string fileType;
public string FileType
{
get { return fileType; }
set { fileType = value; }
}
private string fileState;
public string FileState
{
get { return fileState; }
set { fileState = value; }
}
private string filePath;
public string FilePath
{
get { return filePath; }
set { filePath = value; }
}
private string fileText;
public string FileText
{
get { return fileText; }
set { fileText = value; }
}
}
3读取xml数据到ListView控件中:
private void ShowXML() //显示xml数据
{
string path = "filesAutoxml"; //xml的文件位置
XmlDocument xmlDoc = new XmlDocument();
XmlReaderSettings settings = new XmlReaderSettings();
settingsIgnoreComments = true; //忽略xml文档中的注释
XmlReader reader = XmlReaderCreate(path, settings);
xmlDocLoad(reader); //读取xml数据
XmlNode xn = xmlDocSelectSingleNode("filesauto");
XmlNodeList xnl = xnChildNodes;
List<FilesAuto> stsList = new List<FilesAuto>();
//循环遍历获取xml文档中的全部数据
foreach (XmlNode Xnl in xnl)
{
FilesAuto filesAuto = new FilesAuto();
XmlElement xe1 = (XmlElement)Xnl;
XmlNodeList xnl0 = xe1ChildNodes;
filesAutoFileType = xnl0Item(0)InnerText;
filesAutoFileState = xnl0Item(1)InnerText;
filesAutoFilePath = xnl0Item(2)InnerText;
filesAutoFileText = xnl0Item(3)InnerText;
ListViewItem list = listView1ItemsAdd(filesAutoFileType);
listSubItemsAdd(filesAutoFileState);
listSubItemsAdd(filesAutoFilePath);
listSubItemsAdd(filesAutoFileText);
}
readerClose(); //关闭读取流
}
使用方法:
private void Button6_Click(object sender, EventArgs e)
{
ShowXML();
}
效果图:
若有不足之处恳请大家指出改正
这个要求不需要作XML的解析,用字符串搜索功能就足够了,把网页内容读出之中按字符串搜索就可以找到<lat>和<lng>。
比如让指针 char page 指向读取得到的网页内容,就可以这样得到经度lat和纬度lng:
#include <stringh>
#include <stdioh>
double lat, lng;
char str_lat, str_lng;
str_lat = strstr(page, "<lat>"); /搜索字符串<lat>的位置/
sscanf(str_lat+5, "%lf", &lat); /从搜索到的位置之后读取一个浮点数作为纬度lat/
str_lng = strstr(page, "<lng>");
sscanf(str_lng+5, "%lf", &lng); /类似地,读出经度lng/
以上就是关于C#如何读取xml文件内容并用listview控件显示全部的内容,包括:C#如何读取xml文件内容并用listview控件显示、怎么样C语言解析一个XML文件中的信息,跪求高人指点。、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)