linq to xml 和命名空间

发布于 2024-08-30 15:24:03 字数 589 浏览 2 评论 0原文

我总是很高兴有机会使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两人的回忆 2024-09-06 15:24:04

在我看来,LINQ to XML 中的命名空间处理得非常优雅。这是一个示例:

XNamespace ns1 = "http://cbsv.ssa.gov/ws/datatype";
XDocument doc = XDocument.Load(...);
string code = doc.Descendants(ns1 + "responseCode")
                 .Select(x => (string) x)
                 .First();

如果您想从顶部开始工作,使用涉及的两个命名空间,那也没关系:

XNamespace ns1 = "http://cbsv.ssa.gov/ws/datatype";
XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XDocument doc = XDocument.Load(...);
string code = (string) doc.RootElement
                          .Element(soapenv + "Body")
                          .Element(ns1 + "ActionResponse")
                          .Element(ns1 + "responseCode");

需要明确的是,没有什么强迫您使用与命名空间相同的变量名称XML 中的前缀 - 为了清楚起见,我这样做了。

Namespaces in LINQ to XML are really elegantly handled, IMO. Here's an example:

XNamespace ns1 = "http://cbsv.ssa.gov/ws/datatype";
XDocument doc = XDocument.Load(...);
string code = doc.Descendants(ns1 + "responseCode")
                 .Select(x => (string) x)
                 .First();

If you want to work down from the top, using both of the namespaces involved, that's okay too:

XNamespace ns1 = "http://cbsv.ssa.gov/ws/datatype";
XNamespace soapenv = "http://schemas.xmlsoap.org/soap/envelope/";
XDocument doc = XDocument.Load(...);
string code = (string) doc.RootElement
                          .Element(soapenv + "Body")
                          .Element(ns1 + "ActionResponse")
                          .Element(ns1 + "responseCode");

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.

但可醉心 2024-09-06 15:24:04

对于这样的 XML:

<esri:DataElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.0" xsi:type="esri:DEFeatureClass">
  <CatalogPath>\counties</CatalogPath>
  <Name>counties</Name>

…

</esri:DataElement>

我使用的查询如下:

tableInfo.TableName = (from element in xDoc.Descendants("Name")
                                                   select Convert.ToString(element.Value)).FirstOrDefault();

但是,如果您在代码中定义命名空间,那么您可以获得更具体、更快的结果:

XNamespace esri = "http://www.esri.com/schemas/ArcGIS/10.0";

tableInfo.TableName = xDoc.Element(esri + "DataElement").Element("Name").Value;

您可以考虑将命名空间声明为替换字符串中的“esri:”。 (也无法在查询中使用冒号)。另外,在这样的文件中,我发现标签多次出现,因此获取正确的标签(或至少一组)非常重要。之前,我最终得到了字段的冗余信息,这会导致 SQL Server 表的创建变得混乱。现在我可以定义我想要与文档根相关的项目。

With XML like this:

<esri:DataElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:esri="http://www.esri.com/schemas/ArcGIS/10.0" xsi:type="esri:DEFeatureClass">
  <CatalogPath>\counties</CatalogPath>
  <Name>counties</Name>

…

</esri:DataElement>

I was using queries like:

tableInfo.TableName = (from element in xDoc.Descendants("Name")
                                                   select Convert.ToString(element.Value)).FirstOrDefault();

But if you define the namespace in your code, then you can get more specific, faster:

XNamespace esri = "http://www.esri.com/schemas/ArcGIS/10.0";

tableInfo.TableName = xDoc.Element(esri + "DataElement").Element("Name").Value;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文