c# 获取xml节点

wanglaibing 2009-09-23 02:31:58
<?xml version="1.0" encoding="utf-8"?>
<CRM_Dictionary>
<Service_Score>
<sort>3</sort>
<default>false</default>
<enalbe>false</enalbe>
<content>东北区</content>
</Service_Score>
<Client_Area>
<sort>0 </sort>
<default_>false </default_>
<enable>false </enable>
<content>sdfad </content>
</Client_Area>
<Client_Area>
<sort>1 </sort>
<default_>false </default_>
<enable>true </enable>
<content>asdfsad </content>
</Client_Area>
<Client_Area>
<sort>2 </sort>
<default_>false </default_>
<enable>false </enable>
<content>asdfasd </content>
</Client_Area>
<Client_Area>
<sort>3 </sort>
<default_>true </default_>
<enable>false </enable>
<content>3434343434 </content>
</Client_Area>
</CRM_Dictionary> ---这是我的xml

我如何用vs2008 获取所有节点Client_Area的值
...全文
208 19 打赏 收藏 转发到动态 举报
写回复
用AI写文章
19 条回复
切换为时间正序
请发表友善的回复…
发表回复
mohanzb 2009-09-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 wanglaibing 的回复:]
<%
Set oDoc    = CreateObject("Msxml2.DOMDocument")
With oDoc
    .async              = False
    .validateOnParse    = False
    .preserveWhiteSpace = False
    .resolveExternals  = False
    .load "c:\xxx.xml"
    If .parseError.errorCode <> 0 Then
        sErrMsg    = .parseError.errorCode & "|" &_
                      .parseError.srcText & "|" & .parseError.reason
        Err.Raise 62002, "Create", sErrMsg
    End If
    Set oNodes = .selectNodes("//Client_Area")
    For i=0 To oNodes.length-1
      Set oNode = oNodes(i).selectSingleNode("sort")
      Response.Write oNode.text
      Set oNode = oNodes(i).selectSingleNode("default_")
      Response.Write oNode.text
      Set oNode = oNodes(i).selectSingleNode("enable")
      Response.Write oNode.text
      Set oNode = oNodes(i).selectSingleNode("content")
      Response.Write oNode.text
    Next
End With
%>
这是一位牛哥用asp给我发的,可我好象用C#弄不了,有哪位牛哥把这个改成C#的
[/Quote]
这是C#3.0的LINQ
如果你VS不支持的话,先添加System.Xml.Linq程序集的引用,然后再添加using System.Xml.Linq;
没有这个上面那个DLL可以下个,呵呵
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
是,还是不行!!!
谢大哥你了,我找下别的方法,
用XmlDocument
huming_h 2009-09-23
  • 打赏
  • 举报
回复
你的是web的项目吧。请在Load的地方用 XElement.Load(Server.MapPath("~/Config/CRM_Dictionary.xml"));
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
XElement doc = XElement.Load("../../Config/CRM_Dictionary.xml");
var v = from s in doc.Descendants("Client_Area")
select s.Value;
foreach (string s in v)
{
Console.WriteLine(s);
}
}
}
报:当前上下文不存在 XElement
huming_h 2009-09-23
  • 打赏
  • 举报
回复
你想要什么样的结果???
我晕。我给你的代码不是这个样子的啊
var v = from s in doc.Descendants("Client_Area")
select s;
foreach(string s in v)
{
Console.WriteLine(s);
}
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
labe = repeater1.Items.Count;
XDocument xmlFile = XDocument.Load("../../Config/CRM_Dictionary.xml");
var query = from c in xmlFile.Elements("CRM_Dictionary").Elements("sort")
select c;
foreach (XElement book in query)
{
Console.WriteLine(book.Value.ToString());
}
}
}

这样还是不行,!!!
huming_h 2009-09-23
  • 打赏
  • 举报
回复
不要用XmlDocument,是XElement
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
public manage_AddCRM_CRMDictionary()
{
//构造函数
myDoc = new XmlDocument();
myDoc.Load(Server.MapPath("../../Config/CRM_Dictionary.xml"));
}
我在构造时就加了
huming_h 2009-09-23
  • 打赏
  • 举报
回复
XElement doc = XElement.Load("tt.xml"); //你需要加载xml文件
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class manage_AddCRM_CRMDictionary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack) {
XElement doc = XElement.Load("tt.xml");
var v = from s in myDoc.Descendants("Client_Area")
select s.Value;
foreach (string s in v)
{
Console.WriteLine(s);
}
}
}
}

这个过不去啊!!!
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
引入了大哥
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ZoomLa.Model;
using ZoomLa.BLL;
using ZoomLa.Common;
using System.Xml;
using System.IO;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
using System.Xml.Xsl;
public partial class manage_AddCRM_CRMDictionary : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


if (!IsPostBack) {
labe = repeater1.Items.Count;

var v = from s in myDoc.Descendants("Client_Area")
select s.Value;
foreach (string s in v)
{
Console.WriteLine(s);
}
}
}
}
huming_h 2009-09-23
  • 打赏
  • 举报
回复
如果你没有引用命名空间,肯定是会报错的。我在我本地都运行过了,代码没有问题。
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
大哥
from s in myDoc.Descendants("Client_Area")
这里的Descendants这个过不去
huming_h 2009-09-23
  • 打赏
  • 举报
回复
ling 需要framework3.5的支持
你不是用vs2008建的项目吗?难道没有还是用的framework2.0
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 huming_h 的回复:]
  XElement doc = XElement.Load("tt.xml");
            var v = from s in doc.Descendants("Client_Area")
                    select s.Value;
            foreach(string s in v)
            {
                Console.WriteLine(s);
            }

用linq to xml 来实现
引入命名空间using System.Xml.Linq;
[/Quote]

这个using System.Xml.Linq;我放在程序里报错,说这个命名空间不存在
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
<%
Set oDoc = CreateObject("Msxml2.DOMDocument")
With oDoc
.async = False
.validateOnParse = False
.preserveWhiteSpace = False
.resolveExternals = False
.load "c:\xxx.xml"
If .parseError.errorCode <> 0 Then
sErrMsg = .parseError.errorCode & "|" &_
.parseError.srcText & "|" & .parseError.reason
Err.Raise 62002, "Create", sErrMsg
End If
Set oNodes = .selectNodes("//Client_Area")
For i=0 To oNodes.length-1
Set oNode = oNodes(i).selectSingleNode("sort")
Response.Write oNode.text
Set oNode = oNodes(i).selectSingleNode("default_")
Response.Write oNode.text
Set oNode = oNodes(i).selectSingleNode("enable")
Response.Write oNode.text
Set oNode = oNodes(i).selectSingleNode("content")
Response.Write oNode.text
Next
End With
%>
这是一位牛哥用asp给我发的,可我好象用C#弄不了,有哪位牛哥把这个改成C#的
wanglaibing 2009-09-23
  • 打赏
  • 举报
回复
我查了好多的资料,可没有找到我相同的!!!
帮忙啊
huming_h 2009-09-23
  • 打赏
  • 举报
回复
XElement doc = XElement.Load("tt.xml");
var v = from s in doc.Descendants("Client_Area")
select s.Value;
foreach(string s in v)
{
Console.WriteLine(s);
}

用linq to xml 来实现
引入命名空间using System.Xml.Linq;
liujintaozyc 2009-09-23
  • 打赏
  • 举报
回复
你又用dom
创建一个dom
上网查查
网上比较多

110,534

社区成员

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

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

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