开发者

Read / Write XmlDocument with Namespaces

Consider the following XmlDocument with namespaces:

<Report xmlns="http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition" xmlns:rd="http://schemas.microsoft.com/SQLServer/reporting/reportdesigner">
  <DataSources>
    <DataSource Name="DummyDataSource">
      <rd:DataSourceID>a0a7ff0a-a268-4f7e-b949-9427e308468a</rd:DataSourceID>
      <ConnectionProperties>
        <DataProvider>SQL</DataProvider>
        <ConnectString />
      </ConnectionProperties>
    </DataSource>
  </DataSources>
</Report>

I wrote,

Dim doc As New XmlDocument
doc.Load("c:\MyXml.xml")
Dim nsm As New XmlNamespaceManager(doc.NameTable)
nsm.AddNamespace("", "http://schemas.microsoft.com/sqlserver开发者_如何学C/reporting/2005/01/reportdefinition")
nsm.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner")
Dim xnd As XmlNode = doc.SelectSingleNode("//DataSources/DataSource/...", nsm)
MessageBox.Show(xnd.Name)

I want to read and modify values of following tags (XPath):

  1. //DataSources/DataSource/rd:DataSourceID
  2. //DataSources/DataSource/ConnectionProperties/DataProvider


My preference would be to use LINQ to XML and then read the element using a proper XName instead of a string. Something like:

Dim defaultSpace As XNamespace = XNamespace.Get("http://schemas.microsoft.com/sqlserver/reporting/2005/01/reportdefinition")    
Dim firstSource As XElement = doc.Element(defaultSpace + "DataSources").Element(defaultSpace + "DataSource")


You need to use these XPath formats and include the namespace-uri() - which is a pain in the neck:

//DataSources[namespace-uri()='']/DataSource[namespace-uri()='']/...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜