linq to xml 和命名空间
我总是很高兴有机会使用 linq to xml,然后我遇到了与命名空间相同的 PITA 问题。不知道我出了什么问题,但我永远无法完全理解发生了什么。基本上我只需要获取responseCode元素的值,到目前为止我还没有运气:(
<?xml version="1.0" encoding="IBM437"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:ActionResponse xmlns:ns1="http://cbsv.ssa.gov/ws/datatype">
<ns1:responseCode>0000</ns1:responseCode>
<ns1:responseDescription>Successful</ns1:responseDescription>
</ns1:ActionResponse>
</soapenv:Body>
</soapenv:Envelope>
I'm always so excited to get a chance to use linq to xml and then I run into the same PITA issue with namespaces. Not sure what is wrong with me but I can never quite grok what is going on. Basically I just need to get the value of the responseCode element and so far I have had no luck :(
<?xml version="1.0" encoding="IBM437"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:ActionResponse xmlns:ns1="http://cbsv.ssa.gov/ws/datatype">
<ns1:responseCode>0000</ns1:responseCode>
<ns1:responseDescription>Successful</ns1:responseDescription>
</ns1:ActionResponse>
</soapenv:Body>
</soapenv:Envelope>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,LINQ to XML 中的命名空间处理得非常优雅。这是一个示例:
如果您想从顶部开始工作,使用涉及的两个命名空间,那也没关系:
需要明确的是,没有什么强迫您使用与命名空间相同的变量名称XML 中的前缀 - 为了清楚起见,我这样做了。
Namespaces in LINQ to XML are really elegantly handled, IMO. Here's an example:
If you want to work down from the top, using both of the namespaces involved, that's okay too:
Just to be clear, there's nothing forcing you to use the same variable name as the namespace prefix in the XML - I've just done so for clarity.
对于这样的 XML:
我使用的查询如下:
但是,如果您在代码中定义命名空间,那么您可以获得更具体、更快的结果:
您可以考虑将命名空间声明为替换字符串中的“esri:”。 (也无法在查询中使用冒号)。另外,在这样的文件中,我发现标签多次出现,因此获取正确的标签(或至少一组)非常重要。之前,我最终得到了字段的冗余信息,这会导致 SQL Server 表的创建变得混乱。现在我可以定义我想要与文档根相关的项目。
With XML like this:
I was using queries like:
But if you define the namespace in your code, then you can get more specific, faster:
You can think of declaring the namespace as taking the place of “esri:” in strings. (There’s no way to use a colon in a query either). Also, in a file like this, I was finding multiple occurrences of tags so it’s important to get the right ones (or at least just one set). Before, I was ending up with redundant info for fields, which would mess up creating a SQL Server table. Now I can define which item I want related to the document root.