发布网友 发布时间:2022-04-27 01:42
共4个回答
热心网友 时间:2022-06-22 03:04
xml文件和txt文件相同,使用普通的文本操作函数即可读取。
1、C语言标准库提供了一系列文件操作函数。文件操作函数一般以f+单词的形式来命名(f是file的简写),其声明位于stdio.h头文件当中。例如:fopen、fclose函数用于文件打开与关闭;fscanf、fgets函数用于文件读取;fprintf、fputs函数用于文件写入;ftell、fseek函数用于文件操作位置的获取与设置。
2、例程:
热心网友 时间:2022-06-22 03:05
我上次才给人写过
xml文件内容
<?xml version="1.0" encoding="UTF-8" ?>
- <aicomoa_response>
- <country_list>
- <country>
<id>7</id>
<pid>0</pid>
<continent_id>1</continent_id>
<guohao>93</guohao>
<cntitle>阿富汗</cntitle>
<entitle>Afghanistan</entitle>
<hztitle>阿富汗</hztitle>
<jptitle>アフガニスタン</jptitle>
<kotitle>??????</kotitle>
<jp_pinyin>ア</jp_pinyin>
<pinyin>AFuHan</pinyin>
<sid>0</sid>
<jibie>1</jibie>
</country>
- <country>
<id>8</id>
<pid>0</pid>
<continent_id>2</continent_id>
<guohao>355</guohao>
<cntitle>阿尔巴尼亚</cntitle>
<entitle>Albania</entitle>
<hztitle>阿尔巴尼亚</hztitle>
<jptitle>アルバニア</jptitle>
<kotitle />
<jp_pinyin>ア</jp_pinyin>
<pinyin>AErBaNiYa</pinyin>
<sid>0</sid>
<jibie>1</jibie>
</country>
</country_list>
</aicomoa_response>
运行结果
Info[0]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Info[1]=[id:7|pid:0|continent_id:1|guohao:93|cntitle:阿富汗|entitle:Afghanistan|
hztitle:阿富汗|jptitle:アフガニスタン|kotitle:??????|jp_pinyin:ア|pinyin:AFuHan|
sid:0|jibie:1|]
Press any key to continue
代码
#include <stdio.h>
#include <string.h>
main()
{
int i=0;
FILE *fp;
char szFileBuff[1024] = {0}, szBuff[100][1024];
char id[10] = {0}, pid[10] = {0}, continent_id[10] = {0}, guohao[10] = {0},
cntitle[]= {0},entitle[]= {0},hztitle[] = {0},jptitle[] = {0},
kotitle[] = {0},jp_pinyin[] = {0}, pinyin[] = {0},sid[10] = {0},jibie[10] = {0};
char *lFirst, *lEnd;
fp = fopen("country.txt","r");
if (fp==NULL)
{
printf("read XML file error!\n");
}
while(fgets(szFileBuff, 1023, fp))
{
if ((lFirst = strstr(szFileBuff, "<id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</id>");
memcpy(id, lFirst + 4, lEnd - lFirst - 4);
}
if ((lFirst = strstr(szFileBuff, "<pid>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</pid>");
memcpy(pid, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<continent_id>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</continent_id>");
memcpy(continent_id, lFirst + 14, lEnd - lFirst - 14);
}
if ((lFirst = strstr(szFileBuff, "<guohao>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</guohao>");
memcpy(guohao, lFirst + 8, lEnd - lFirst - 8);
}
if ((lFirst = strstr(szFileBuff, "<cntitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</cntitle>");
memcpy(cntitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<entitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</entitle>");
memcpy(entitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<hztitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</hztitle>");
memcpy(hztitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<jptitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jptitle>");
memcpy(jptitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<kotitle>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</kotitle>");
memcpy(kotitle, lFirst + 9, lEnd - lFirst - 9);
}
if ((lFirst = strstr(szFileBuff, "<jp_pinyin>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jp_pinyin>");
memcpy(jp_pinyin, lFirst + 11, lEnd - lFirst - 11);
}
if ((lFirst = strstr(szFileBuff, "<pinyin>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</pinyin>");
memcpy(pinyin, lFirst + 8, lEnd - lFirst - 8);
}
if ((lFirst = strstr(szFileBuff, "<sid>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</sid>");
memcpy(sid, lFirst + 5, lEnd - lFirst - 5);
}
if ((lFirst = strstr(szFileBuff, "<jibie>")) != NULL)
{
lEnd = strstr(lFirst + 1, "</jibie>");
memcpy(jibie, lFirst + 7, lEnd - lFirst - 7);
}
if ((lFirst = strstr(szFileBuff, "</country>")) != NULL)
{
sprintf(szBuff[i],"id:%s|pid:%s|continent_id:%s|guohao:%s|cntitle:%s|entitle:%s|hztitle:%s|jptitle:%s|kotitle:%s|jp_pinyin:%s|pinyin:%s|sid:%s|jibie:%s|",
id,pid,continent_id,guohao,cntitle,entitle,hztitle,jptitle,kotitle,jp_pinyin, pinyin,sid,jibie);
printf("Info[%d]=[%s]\n",i++, szBuff);
}
}
fclose(fp);
}
热心网友 时间:2022-06-22 03:05
用System.xml命名空间
热心网友 时间:2022-06-22 03:06
/**/
/// <summary>
/// XML 操作基类
/// </summary>
public class XmlHelper
{
/**/
/// <summary>
/// 读取Xml到DataSet中
/// </summary>
/// <param name="XmlPath">路径</param>
/// <returns>结果集</returns>
public static DataSet GetXml(string XmlPath)
{
DataSet ds = new DataSet();
ds.ReadXml(@XmlPath);
return ds;
}
/**/
/// <summary>
/// 读取xml文档并返回一个节点:适用于一级节点
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <param name="NodeName">节点</param>
/// <returns></returns>
public static string ReadXmlReturnNode(string XmlPath, string Node)
{
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn = docXml.GetElementsByTagName(Node);
return xn.Item(0).InnerText.ToString();
}
/**/
/// <summary>
/// 查找数据,返回当前节点的所有下级节点,填充到一个DataSet中
/// </summary>
/// <param name="xmlPath">xml文档路径</param>
/// <param name="XmlPathNode">节点的路径:根节点/父节点/当前节点</param>
/// <returns></returns>
public static DataSet GetXmlData(string xmlPath, string XmlPathNode)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
DataSet ds = new DataSet();
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(XmlPathNode).OuterXml);
ds.ReadXml(read);
return ds;
}
/**/
/// <summary>
/// 更新Xml节点内容
/// </summary>
/// <param name="xmlPath">xml路径</param>
/// <param name="Node">要更换内容的节点:节点路径 根节点/父节点/当前节点</param>
/// <param name="Content">新的内容</param>
public static void XmlNodeReplace(string xmlPath, string Node, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
objXmlDoc.SelectSingleNode(Node).InnerText = Content;
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 更改节点的属性值
/// </summary>
/// <param name="xmlPath">文件路径</param>
/// <param name="NodePath">节点路径</param>
/// <param name="NodeAttribute1">要更改的节点属性的名称</param>
/// <param name="NodeAttributeText">更改的属性值</param>
public static void XmlAttributeEdit(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 删除XML节点和此节点下的子节点
/// </summary>
/// <param name="xmlPath">xml文档路径</param>
/// <param name="Node">节点路径</param>
public static void XmlNodeDelete(string xmlPath, string Node)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
string mainNode = Node.Substring(0, Node.LastIndexOf("/"));
objXmlDoc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 删除一个节点的属性
/// </summary>
/// <param name="xmlPath">文件路径</param>
/// <param name="NodePath">节点路径(xpath)</param>
/// <param name="NodeAttribute">属性名称</param>
public static void xmlnNodeAttributeDel(string xmlPath, string NodePath, string NodeAttribute)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
XmlElement xe = (XmlElement)nodePath;
xe.RemoveAttribute(NodeAttribute);
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 插入一个节点和此节点的子节点
/// </summary>
/// <param name="xmlPath">xml路径</param>
/// <param name="MailNode">当前节点路径</param>
/// <param name="ChildNode">新插入节点</param>
/// <param name="Element">插入节点的子节点</param>
/// <param name="Content">子节点的内容</param>
public static void XmlInsertNode(string xmlPath, string MailNode, string ChildNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objRootNode = objXmlDoc.SelectSingleNode(MailNode);
XmlElement objChildNode = objXmlDoc.CreateElement(ChildNode);
objRootNode.AppendChild(objChildNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objChildNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 向一个节点添加属性
/// </summary>
/// <param name="xmlPath">xml文件路径</param>
/// <param name="NodePath">节点路径</param>
/// <param name="NodeAttribute1">要添加的节点属性的名称</param>
/// <param name="NodeAttributeText">要添加属性的值</param>
public static void AddAttribute(string xmlPath, string NodePath, string NodeAttribute1, string NodeAttributeText)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlAttribute nodeAttribute = objXmlDoc.CreateAttribute(NodeAttribute1);
XmlNode nodePath = objXmlDoc.SelectSingleNode(NodePath);
nodePath.Attributes.Append(nodeAttribute);
XmlElement xe = (XmlElement)nodePath;
xe.SetAttribute(NodeAttribute1, NodeAttributeText);
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 插入一节点,带一属性
/// </summary>
/// <param name="xmlPath">Xml文档路径</param>
/// <param name="MainNode">当前节点路径</param>
/// <param name="Element">新节点</param>
/// <param name="Attrib">属性名称</param>
/// <param name="AttribContent">属性值</param>
/// <param name="Content">新节点值</param>
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Attrib, string AttribContent, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.SetAttribute(Attrib, AttribContent);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 插入一节点,不带属性
/// </summary>
/// <param name="xmlPath">Xml文档路径</param>
/// <param name="MainNode">当前节点路径</param>
/// <param name="Element">新节点</param>
/// <param name="Content">新节点值</param>
public static void XmlInsertElement(string xmlPath, string MainNode, string Element, string Content)
{
XmlDocument objXmlDoc = new XmlDocument();
objXmlDoc.Load(xmlPath);
XmlNode objNode = objXmlDoc.SelectSingleNode(MainNode);
XmlElement objElement = objXmlDoc.CreateElement(Element);
objElement.InnerText = Content;
objNode.AppendChild(objElement);
objXmlDoc.Save(xmlPath);
}
/**/
/// <summary>
/// 在根节点下添加父节点
/// </summary>
public static void AddParentNode(string xmlPath, string parentNode)
{
XmlDocument xdoc = new XmlDocument();
xdoc.Load(xmlPath);
// 创建一个新的menber节点并将它添加到根节点下
XmlElement Node = xdoc.CreateElement(parentNode);
xdoc.DocumentElement.PrependChild(Node);
xdoc.Save(xmlPath);
}
/**/
/// <summary>
/// 根据节点属性读取子节点值(较省资源模式)
/// </summary>
/// <param name="XmlPath">xml路径</param>
/// <param name="FatherElement">父节点值</param>
/// <param name="AttributeName">属性名称</param>
/// <param name="AttributeValue">属性值</param>
/// <param name="ArrayLength">返回的数组长度</param>
/// <returns></returns>
public static System.Collections.ArrayList GetSubElementByAttribute(string XmlPath, string FatherElement, string AttributeName, string AttributeValue, int ArrayLength)
{
System.Collections.ArrayList al = new System.Collections.ArrayList();
XmlDocument docXml = new XmlDocument();
docXml.Load(@XmlPath);
XmlNodeList xn;
xn = docXml.DocumentElement.SelectNodes("//" + FatherElement + "[" + @AttributeName + "='" + AttributeValue + "']");
XmlNodeList xx = xn.Item(0).ChildNodes;
for (int i = 0; i < ArrayLength & i < xx.Count; i++)
{
al.Add(xx.Item(i).InnerText);
}
return al;
}