Redian新闻
>
心得:use XPath (+namespace)
avatar
心得:use XPath (+namespace)# DotNet - 窗口里的风景
c*t
1
因为工作的需要,要parse非常复杂的xml.用donet来parse XML非常方便,尤其用XPath
直接选取node(s)。但是当xml比较复杂的时候,比如从crystal reports直接导出的xml
,通常的办法:
XmlDocument xml = new XmlDocument();
xml.Load( PathToXmlFile );
XmlNodeList selection = xml.SelectNodes(strXPathExpression);
就行不通:明明xml.innerXml不空,selection.Count总是0。去
掉namespace就可以了。经过一番Google,终于找到答案:在SelectNodes前加上
XmlNamespaceManager nsmgr = new XmlNamespanceManager(xml.NameTable);
nsmgr.AddNamespace("a", "http://....");
nsmgr.AddNamespace("b", "urn:....");
XmlNodeList selection =
avatar
d*d
2
有没有试过Linq to Xml?

XPath
xml

【在 c**t 的大作中提到】
: 因为工作的需要,要parse非常复杂的xml.用donet来parse XML非常方便,尤其用XPath
: 直接选取node(s)。但是当xml比较复杂的时候,比如从crystal reports直接导出的xml
: ,通常的办法:
: XmlDocument xml = new XmlDocument();
: xml.Load( PathToXmlFile );
: XmlNodeList selection = xml.SelectNodes(strXPathExpression);
: 就行不通:明明xml.innerXml不空,selection.Count总是0。去
: 掉namespace就可以了。经过一番Google,终于找到答案:在SelectNodes前加上
: XmlNamespaceManager nsmgr = new XmlNamespanceManager(xml.NameTable);
: nsmgr.AddNamespace("a", "http://....");

avatar
l*s
3
最近用profiler看了一下程序,发现Linq.Mapping.AttributeMappingSource占了1M的
内存。还没看懂生存周期是多长。有没有办法降低呢?

【在 d****d 的大作中提到】
: 有没有试过Linq to Xml?
:
: XPath
: xml

avatar
d*d
4
你用的是class attributes定义的mapping?你的mapping有多大规模?
我觉得mapping还是应该长留内存吧,因为DataContext本身是short lived,
如果每次都重新生成一个mapping然后再生成DataContext,可能会更慢。

【在 l*s 的大作中提到】
: 最近用profiler看了一下程序,发现Linq.Mapping.AttributeMappingSource占了1M的
: 内存。还没看懂生存周期是多长。有没有办法降低呢?

avatar
l*s
5
用的是LinqToSQl的designer自动生成的tables和stored procedures的mapping.因为
datacontext是short lived. 估计这些mapping也是short lived。1M还是太大了点。

【在 d****d 的大作中提到】
: 你用的是class attributes定义的mapping?你的mapping有多大规模?
: 我觉得mapping还是应该长留内存吧,因为DataContext本身是short lived,
: 如果每次都重新生成一个mapping然后再生成DataContext,可能会更慢。

avatar
d*d
6
mapping不应该是short lived吧,否则每次生成datacontext都要重新建立一遍mapping
太慢了。

【在 l*s 的大作中提到】
: 用的是LinqToSQl的designer自动生成的tables和stored procedures的mapping.因为
: datacontext是short lived. 估计这些mapping也是short lived。1M还是太大了点。

avatar
c*t
7
用Linq也许要加上namespace:
XNamespace ns = "urn:crystal-reports:schema";
var records = from r in xml.Document.Descendants(ns+"NameTag") select r;
...

【在 d****d 的大作中提到】
: 有没有试过Linq to Xml?
:
: XPath
: xml

avatar
k*e
8
这个跟用什么都没有关系,如果xml里面有namesplace,
xpath当然也要。

XPath
xml

【在 c**t 的大作中提到】
: 因为工作的需要,要parse非常复杂的xml.用donet来parse XML非常方便,尤其用XPath
: 直接选取node(s)。但是当xml比较复杂的时候,比如从crystal reports直接导出的xml
: ,通常的办法:
: XmlDocument xml = new XmlDocument();
: xml.Load( PathToXmlFile );
: XmlNodeList selection = xml.SelectNodes(strXPathExpression);
: 就行不通:明明xml.innerXml不空,selection.Count总是0。去
: 掉namespace就可以了。经过一番Google,终于找到答案:在SelectNodes前加上
: XmlNamespaceManager nsmgr = new XmlNamespanceManager(xml.NameTable);
: nsmgr.AddNamespace("a", "http://....");

avatar
c*t
9
Yes, you are right. If I'm not asked to do such a job, I might never know ab
out this.

【在 k***e 的大作中提到】
: 这个跟用什么都没有关系,如果xml里面有namesplace,
: xpath当然也要。
:
: XPath
: xml

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。