1
我正在努力研究如何使用默認命名空間與XmlDataProvider和XPath綁定。XmlDataProvider和XPath綁定不允許XML數據的默認名稱空間?
有一個ugly answer使用本地名稱<Binding XPath="*[local-name()='Name']" />
但這是客戶誰不希望這個XAML高度維護。
回退是強制他們在報表XML中使用非默認名稱空間,但這是一個不理想的解決方案。
XML報告文件如下所示。它只會在我刪除xmlns="http://www.acme.com/xml/schemas/report
時才起作用,所以沒有默認名稱空間。
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type='text/xsl' href='PreviewReportImages.xsl'?>
<Report xsl:schemaLocation="http://www.acme.com/xml/schemas/report BlahReport.xsd" xmlns:xsl="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.acme.com/xml/schemas/report">
<Service>Muncher</Service>
<Analysis>
<Date>27 Apr 2010</Date>
<Time>0:09</Time>
<Authoriser>Service Centre Manager</Authoriser>
對此我在一個窗口中使用XAML呈現:
<Window x:Class="AcmeTest.ReportPreview"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="ReportPreview" Height="300" Width="300" >
<Window.Resources>
<XmlDataProvider x:Key="Data"/>
</Window.Resources>
<StackPanel Orientation="Vertical" DataContext="{Binding Source={StaticResource Data}, XPath=Report}">
<TextBlock Text="{Binding XPath=Service}"/>
</StackPanel>
</Window>
與代碼隱藏用於一個XmlDocument加載到XmlDataProvider(似乎有從文件加載或對象變化的唯一途徑在運行時)。
public partial class ReportPreview : Window
{
private void InitXmlProvider(XmlDocument doc)
{
XmlDataProvider xd = (XmlDataProvider)Resources["Data"];
xd.Document = doc;
}
public ReportPreview(XmlDocument doc)
{
InitializeComponent();
InitXmlProvider(doc);
}
public ReportPreview(String reportPath)
{
InitializeComponent();
var doc = new XmlDocument();
doc.Load(reportPath);
InitXmlProvider(doc);
}
}
沒有測試,您應該能夠使用XmlNamespaceManager作爲綁定的一部分;那麼你將不得不使用一個前綴,但它應該工作。 – p00ya 2010-05-10 04:25:50
是的,強制前綴是我的後備,但這意味着很多XSLT外部腳本需要更改,而且默認名稱空間很滿意。 – 2010-05-10 05:29:21
doh - 看到我的答案在下面! – 2010-05-10 23:02:09