【发布时间】:2014-09-06 14:31:03
【问题描述】:
我有这个文件要解析:
<?xml version="1.0" encoding="utf-8"?>
<Schema Namespace="DataModel.Store" Alias="Self" Provider="System.Data.SqlServerCe.3.5" ProviderManifestToken="3.5" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="DataModelStoreContainer">
<EntitySet Name="erpBarcode" EntityType="DataModel.Store.erpBarcode" store:Type="Tables" />
...
...
</EntityContainer>
<EntityType Name="erpBarcode">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="uniqueidentifier" Nullable="false" />
<Property Name="Barcode" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="ProductNo" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="UnitNo" Type="int" Nullable="false" />
<Property Name="IsActive" Type="bit" Nullable="false" />
</EntityType>
<EntityType Name="erpCustomer">
...
</EntityType>
<EntityType Name="erpOperation">
...
</EntityType>
...
</Schema>
我想从文档中检索所有 EntityType 节点。 首先,我尝试了XPathVisualizer 中的 XPath 表达式,它正确检索了所有节点。问题是当我转向 C# 时。以上是我正在使用的代码,我不知道错误在哪里,因为节点列表总是空的。
var xmldoc = new XmlDocument();
xmldoc.Load(_fileName);
var xmlnsManager = new XmlNamespaceManager(xmldoc.NameTable);
//Add the namespaces used to the XmlNamespaceManager
xmlnsManager.AddNamespace("ns1", "http://schemas.microsoft.com/ado/2009/02/edm/ssdl");
xmlnsManager.AddNamespace("store", "http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator");
var xpath = @"//ns1:Schema/EntityType";
XmlNodeList nodeList = xmldoc.SelectNodes(xpath, xmlnsManager);
foreach (XmlNode node in nodeList)
{
Console.WriteLine(node.InnerText);
}
【问题讨论】:
标签: c# xpath namespaces