try..string strXML = "<?xml version='1.0' encoding='gb2312' ?><Root>";
strXML += "<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML += "<ItemNode 姓名='张三' 批号='2' ></ItemNode></Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
XmlNodeList nodes = doc.SelectNodes(@"//ItemNode");
foreach (XmlNode node in nodes)
{
Console.WriteLine("姓名=" + node.Attributes["姓名"].Value + "\t" + "批号=" + node.Attributes["批号"].Value);
}
strXML += "<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML += "<ItemNode 姓名='张三' 批号='2' ></ItemNode></Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
XmlNodeList nodes = doc.SelectNodes(@"//ItemNode");
foreach (XmlNode node in nodes)
{
Console.WriteLine("姓名=" + node.Attributes["姓名"].Value + "\t" + "批号=" + node.Attributes["批号"].Value);
}
解决方案 »
- C# 同步、异步操作
- C#无法更新数据库的问题
- 请问怎么设定鼠标在一定区域的响应事件?
- .Net能不能用来开发单片机程序?
- 從 System.String 至 System.Guid 的轉換無效 高分幫忙看一下
- string.Format("{0:F2}",Arear)什么意思?
- 请教一下,在.Net下,如何获得在其他进程中的NotifyIcon实例在托盘区的子窗口的句柄
- c# 定时器的使用,,可以返回一个值么
- 如何将数据库中某一字段的数据项添加到ListBox或comboBox的下拉菜单中。
- c#怎么关word?
- listview的简单问题,在线等
- 我是已经没分的菜鸟,我真成的提问,等着你真诚的回答,谢谢!
DataSet.ReadXml 方法
string strXML="?xml version='1.0' encoding='gb2312' ?><Root>";
strXML+="<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML+="<ItemNode 姓名='张三' 批号='2' ></itemNode></Root>"; 如我要获得,姓名这个列名,我先只能获得值,不能获得列名~~~请各位大哥帮忙解决一下~~~
using System.Xml;namespace prjCx
{
/// <summary>
/// Config 的摘要说明。
/// </summary>
public class Config
{
private String msFileName = null; public String ConfigFile
{
get
{
return this.msFileName;
}
set
{
if (System.IO.File.Exists(value.Trim()))
{
this.msFileName = value.Trim();
}
}
} public Config()
{
this.msFileName = String.Empty;
} public Config(String ConfigFile)
{
this.ConfigFile = ConfigFile.Trim();
} public bool ReadConfig(String ContentName, out String ContentValue)
{
bool bFlag = false; ContentValue = String.Empty; if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
ContentValue = xmlNode.InnerText; bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
}
/// <summary>
/// 读XML文件
/// </summary>
/// <param name="ContentName">节点名</param>
/// <param name="PropertyName">属性名</param>
/// <param name="PropertyValue">属性值(Ref)</param>
/// <returns></returns>
public bool ReadConfig(String ContentName, String PropertyName, out String PropertyValue)
{
bool bFlag = false; PropertyValue = String.Empty; if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName); XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); XmlAttributeCollection xmlAttr = xmlNode.Attributes; for (int i = 0; i < xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
PropertyValue = xmlAttr.Item(i).Value;
bFlag = true;
break;
}
}
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
} public bool WriteConfig(String ContentName, String ContentValue)
{
bool bFlag = false; if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
System.Xml.XmlDocument xmlDoc = new System.Xml.XmlDocument();
xmlDoc.Load(this.msFileName);
System.Xml.XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName);
xmlNode.InnerText = ContentValue; xmlDoc.Save(this.msFileName); bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
}
/// <summary>
/// 写XML文件
/// </summary>
/// <param name="ContentName">节点名</param>
/// <param name="PropertyName">属性名</param>
/// <param name="PropertyValue">属性值</param>
/// <returns></returns>
public bool WriteConfig(String ContentName, String PropertyName, String PropertyValue)
{
bool bFlag = false; if (!System.IO.File.Exists(this.msFileName))
{
return bFlag;
} try
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(this.msFileName); XmlNode xmlNode = xmlDoc.SelectSingleNode(ContentName); XmlAttributeCollection xmlAttr = xmlNode.Attributes; for (int i = 0; i < xmlAttr.Count; ++i)
{
if (xmlAttr.Item(i).Name == PropertyName)
{
xmlAttr.Item(i).Value = PropertyValue;
bFlag = true;
break;
}
} xmlDoc.Save(this.msFileName); bFlag = true;
}
catch (XmlException xmle)
{
System.Console.WriteLine(xmle.Message);
} return bFlag;
}
}
}
string strXML="?xml version='1.0' encoding='gb2312' ?><Root>";
strXML+="<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML+="<ItemNode 姓名='张三' 批号='2' ></itemNode></Root>"; 如我要获得,姓名这个列名,我先只能获得值,不能获得列名~~~for example:string strXML = "<?xml version='1.0' encoding='gb2312' ?><Root>";
strXML += "<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML += "<ItemNode 姓名='张三' 批号='2' ></ItemNode></Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
XmlNodeList nodes = doc.SelectNodes(@"//ItemNode");
foreach (XmlNode node in nodes)
{
foreach (XmlAttribute attribute in node.Attributes)
{
Console.Write(attribute.Name + "=" + attribute.Value + "\t");
}
Console.WriteLine();
}输出:
姓名=胡军 批号=1
姓名=张三 批号=2
string strXML="?xml version='1.0' encoding='gb2312' ?><Root>";
strXML+="<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML+="<ItemNode 姓名='张三' 批号='2' ></itemNode></Root>"; 如我要获得,姓名这个列名,我先只能获得值,不能获得列名~~~for example:string strXML = "<?xml version='1.0' encoding='gb2312' ?><Root>";
strXML += "<ItemNode 姓名='胡军' 批号='1' ></ItemNode>";
strXML += "<ItemNode 姓名='张三' 批号='2' ></ItemNode></Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
XmlNodeList nodes = doc.SelectNodes(@"//ItemNode");
foreach (XmlNode node in nodes)
{
foreach (XmlAttribute attribute in node.Attributes)
{
Console.Write(attribute.Name + "=" + attribute.Value + "\t");
}
Console.WriteLine();
}输出:
姓名=胡军 批号=1
姓名=张三 批号=2