XmlDataProvider has inline XML that does not explicitly set its XmlNamespace
I've been playing around with XmlDataProvider with inline XML. Here's my code:
<XmlDataProvider x:Key="InternalData" XPath="/Workspace">
<x:XData>
<Workspace xmlns="" Name="Workspace">
<Project Name="Project 1" />
<Project Name="Project 2" />
<Project Name="Project 3" />
</Workspace>
</x:XData>
</XmlDataProvider>
This is very similar to most of the examples I've seen using inline XML but I keep getting the error "XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="")". I know it's obvious but why would I get this error at all? It should be valid, no? And no one else seems to be having this issue,开发者_StackOverflow中文版 not when I search Google anyway.
Thanks in advance
Update: For further clarification, this error is only showing up in my Output window. It's not a compiler error and it doesn't prevent me from running my program. I can read the XML fine and display it in a TreeView. I'd just like to understand why I get the error in the first place.
For what it's worth, I had exactly the same error some time back, on exactly the same MSDN article (see http://blog.wouldbetheologian.com/2009/07/why-wpf-databinding-is-awful-technology.html for my griping about it). But as to why it's required: I'm completely mystified. It doesn't seem like it should be, since using xmlns="" and leaving it out should have the same effect. My guess is that it's some bizarre artifact of Microsoft's XAML-parsing engine (which of course is not precisely XML-compliant). Perhaps this artifact is intended (a feature), perhaps not (a bug).
Your example works for me. If I use it with the example of the XmlDataProvider MSDN page by using this XAML:
<StackPanel>
<StackPanel.Resources>
<XmlDataProvider x:Key="InternalData" XPath="/Workspace">
<x:XData>
<Workspace xmlns="" Name="Workspace">
<Project Name="Project 1" />
<Project Name="Project 2" />
<Project Name="Project 3" />
</Workspace>
</x:XData>
</XmlDataProvider>
</StackPanel.Resources>
<TextBlock FontSize="18" FontWeight="Bold" Margin="10"
HorizontalAlignment="Center">XML Data Source Sample</TextBlock>
<ListBox
Width="400" Height="300" Background="Honeydew">
<ListBox.ItemsSource>
<Binding Source="{StaticResource InternalData}" XPath="Project" />
</ListBox.ItemsSource>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock FontSize="12" Foreground="Red">
<TextBlock.Text>
<Binding XPath="@Name"/>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</StackPanel>
I get this result:
Well using the xmlns="" to default a namespace is valid
The attribute value in a default namespace declaration MAY be empty. This has the same effect, within the scope of the declaration, of there being no default namespace.
http://www.w3.org/TR/REC-xml-names/#defaulting
Though I'd suggest you may wish to look at your undeclared namespace prefix x:
Namespace constraint: Prefix Declared
The namespace prefix, unless it is xml or xmlns, MUST have been declared in a namespace declaration attribute in either the start-tag of the element where the prefix is used or in an ancestor element (i.e., an element in whose content the prefixed markup occurs).
I had the same issue... realized I was using in the XmlDataProvider and consequently set the namespace property in the tag as follows:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
This is the same as the xmlns:x in my Window tag so it seems redundant, but maybe it's because the xaml parser is handling this XmlDataProvider as a seperate XML document, and it needs it's namespace in this document too? I don't know but at least the error doesn't happen anymore.
精彩评论