<?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的值
<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的值
创建一个dom
上网查查
网上比较多
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;
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#的
这个using System.Xml.Linq;我放在程序里报错,说这个命名空间不存在
你不是用vs2008建的项目吗?难道没有还是用的framework2.0
from s in myDoc.Descendants("Client_Area")
这里的Descendants这个过不去
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);
}
}
}
}
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);
}
}
}
}这个过不去啊!!!
{
//构造函数
myDoc = new XmlDocument();
myDoc.Load(Server.MapPath("../../Config/CRM_Dictionary.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());
}
}
}这样还是不行,!!!
我晕。我给你的代码不是这个样子的啊
var v = from s in doc.Descendants("Client_Area")
select s;
foreach(string s in v)
{
Console.WriteLine(s);
}
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
谢大哥你了,我找下别的方法,
用XmlDocument
如果你VS不支持的话,先添加System.Xml.Linq程序集的引用,然后再添加using System.Xml.Linq;
没有这个上面那个DLL可以下个,呵呵