4

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, 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.

dandax
  • 1,267
  • 3
  • 15
  • 22

4 Answers4

1

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).

Ken Smith
  • 20,305
  • 15
  • 100
  • 147
  • it's funny because your article is one of the only two that show up in Google. I agree with you. I'm completely mystified as well. Surprising there's very little information about it but in any case I did leave a comment on the MSDN article. Let's see if anyone notices. Cheers. – dandax Apr 01 '11 at 21:31
0

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.

Shawn
  • 1
0

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).

James Walford
  • 2,953
  • 1
  • 24
  • 37
0

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:

enter image description here

Gimno
  • 6,506
  • 3
  • 39
  • 39
  • I tried the exact example from the MSDN article just before posting my question and I got the same error. Strange. What version of VS are you using? And if it's 2010 is it SP1? – dandax Apr 01 '11 at 13:46
  • 1
    I am using VS2010 without SP1, but I doubt that this is a VS issue. (it works in kaxaml as well). In the output window I get the error too, `System.Windows.Data Error: 49 : XmlDataProvider has inline XML that does not explicitly set its XmlNamespace (xmlns="").` – Gimno Apr 01 '11 at 13:56