0

I'm using XDocument and LINQ to parse some XML files, and some worked fine, but others did not. Looking into it, I found the difference:

Files that work look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Scenarios xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Scenario>
        ...
    </Scenario>
</Scenarios>

Files that don't look like this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Scenarios xmlns="http://www.w3.org/2001/XMLSchema-instance">
    <Scenario>
        ...
    </Scenario>
</Scenarios>

If you don't see it, the missing element is the :xsi alias in the root element.

I just want to know why this is the case. Files that are otherwise the same won't be parsed by XDocument.Load() unless they have that alias.

Thanks in advance!

1
  • What do you mean, “won't be parsed”? What exactly happens? Does it throw some exception? Do you have any elements that use the xsi: prefixed in the XML that doesn't work? Commented Oct 28, 2011 at 0:49

1 Answer 1

2

In the first example, you are saying that any node (element or attribute) with the xsi prefix is from the XML schema namespace.

In the second example, you are saying that any node without any prefix is in the XML schema namespace.

You either need to make the declaration like the first if you have nodes with the xsi prefix or remove those nodes.

EDIT: As svick correctly noted, XDocument does not perform schema validation by default, so the issue is more likely that the ... contains some nodes with an xsi prefix

Sign up to request clarification or add additional context in comments.

2 Comments

You're mostly right, but I don't think XDocument actually validates the document according to the schema, unless you explicitly validate it yourself.
@svick I had not actually checked that it validated. Answer updated to give a more likely reason.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.