C#读取XML指定节点

gaopeng1988 2010-04-02 12:03:30

<?xml version="1.0" encoding="utf-8" ?>
<Dpycms>
<!--配置数据库连接-->
<Database>
<sqlServer value="server=(local);database=dpycms;user=sa;password=sa"></sqlServer>
</Database>
<att>
<attric value="server baidu option"></attric>
</att>
</Dpycms>


我想写一个方法,专门读取指定的节点的value。
比如一个方法

static string getValue(string strValue)
{
string svalue="";
.....
return svalue;
}

我想从外部调用这个方法,给的参数如"Dpycms.Database.sqlServer".
不知道方法中该怎样写,请高手附上源代码。
...全文
647 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
guyehanxinlei 2010-04-02
  • 打赏
  • 举报
回复

XmlDocument doc = new XmlDocument();
doc.Load(@"c:\xml.xml");
XmlNode Node = doc.GetElementsByTagName("sqlServer")[0];

Console.WriteLine(Node.Attributes["value"].Value);
Console.ReadLine();
gaopeng1988 2010-04-02
  • 打赏
  • 举报
回复
分不够可以再加。快点来个厉害的朋友帮帮忙吧
gaopeng1988 2010-04-02
  • 打赏
  • 举报
回复
自己再顶
gaopeng1988 2010-04-02
  • 打赏
  • 举报
回复
快来人啊
MOTA 2010-04-02
  • 打赏
  • 举报
回复

Read(XmlPath,"/Dpycms/Database/sqlServer","value");
<?xml version="1.0" encoding="utf-8" ?>
<Dpycms>
<!--配置数据库连接-->
<Database>
<sqlServer value="server=(local);database=dpycms;user=sa;password=sa"></sqlServer>
</Database>
<att>
<attric value="server baidu option"></attric>
</att>
</Dpycms>

/// <summary>
/// 读取数据
/// </summary>
/// <param name="path">路径</param>
/// <param name="node">节点</param>
/// <param name="attribute">属性名,非空时返回该属性值,否则返回串联值</param>
/// <returns>string</returns>
/**************************************************
* 使用示列:
* XmlHelper.Read(path, "/Node", "")
* XmlHelper.Read(path, "/Node/Element[@Attribute='Name']", "Attribute")
************************************************/
public static string Read(string path, string node, string attribute)
{
string value = "";
try
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNode xn = doc.SelectSingleNode(node);
value = (attribute.Equals("") ? xn.InnerText : xn.Attributes[attribute].Value);
}
catch { }
return value;
}
cjcgy 2010-04-02
  • 打赏
  • 举报
回复
lz如果用的是一个结构良好的xml, 而且经常进行读操作的话,
那么可以用DataSet读出来。

DataSet用着就习惯多了。
raycdut 2010-04-02
  • 打赏
  • 举报
回复
public string getValue(string strValue)
{
string svalue ="";

XmlDocument doc = new XmlDocument();
doc.LoadXml("");//your xml file

if (doc.HasChildNodes)
{

foreach (XmlNode node in doc.ChildNodes)
{
svalue=findNode(node, strValue);
}
}
return svalue;
}

private string findNode(XmlNode node,string strValue)
{
string svalue = "";

if (node.Name == strValue)
{
svalue = node.Value;
}
else
{
foreach (XmlNode _node in node.ChildNodes)
{
svalue = findNode(node, strValue);
}
}
return svalue;
}
你可以试试,如果不行自行修改下。。
lyonyf 2010-04-02
  • 打赏
  • 举报
回复
使用Xpath进行查找:xmlDoc.SelectNodes("/Dpycms/Database/sqlServer");
Python 2010-04-02
  • 打赏
  • 举报
回复
使用XPath路径进行查找
「已注销」 2010-04-02
  • 打赏
  • 举报
回复
string value= XmlDocument.SelectSingleNode("Xpath路径")
codeingsky 2010-04-02
  • 打赏
  • 举报
回复
public static void SetKeyValue(string AppKey, string AppValue)
{
XmlDocument xDoc = new XmlDocument();

xDoc.Load(System.Windows.Forms.Application.ExecutablePath + ".config");

XmlNode xNode;

XmlElement xElem1;//XmlElement xElem2;

xNode = xDoc.SelectSingleNode("//appSettings");

xElem1 = (XmlElement)xNode.SelectSingleNode("//add[@key='" + AppKey + "']");

if (xElem1 != null)
{
xElem1.SetAttribute("value", AppValue);
}
//else
//{
// xElem2 = xDoc.CreateElement("add");
// xElem2.SetAttribute("name", AppKey);
// xElem2.SetAttribute("connectionString", AppValue);
// xNode.AppendChild(xElem2);
//}
xDoc.Save(System.Windows.Forms.Application.ExecutablePath + ".config");
}


app.config是xml文件吧!

读取指定节点的方法
gsq_0912 2010-04-02
  • 打赏
  • 举报
回复
幫頂下。以前做過的!!
yxiao0302 2010-04-02
  • 打赏
  • 举报
回复
有没有做过XML NOTEPAD的呀?
wzp144650 2010-04-02
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load(@"D:\My Documents\Visual Studio 2005\Projects\ConsoleApplication11\ConsoleApplication11\XMLFile1.xml");
XmlElement root= doc.DocumentElement;
string value= root.SelectNodes("//sqlServer")[0].Attributes["value"].Value;

}

110,499

社区成员

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

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

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