问题
I have the following code that I wish to use to select all the elements I will need in a certain sequence. Here's the snippet:
XmlDocument schema = new XmlDocument();
schema.Load(SchemaFileName);
XmlNamespaceManager xnm = new XmlNamespaceManager(schema.NameTable);
xnm.AddNamespace("xs", "http://www.w3.org/2001/XMLSchema");
XmlNodeList list = schema.SelectNodes(Path);
However, I'm not sure what I should write as the path. Ideally I want to select all the child nodes of the "sequence" tag, but when I set the Path to "sequence", that doesn't give me anything when I run it. The nodelist is just blank. What I'm trying to do is get the names of the elements that I will need (in order) for the validation of an xml file.
Additionally, when I set the Path as "//@name", I do get something, however, that selects all of the elements with "name" as an attribute. The ones I want are specifically right after the "sequence" tag.
I've also tried setting the Path as "xs:sequence", but that gives me an error: "Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function." Which is weird because I thought I set it up already..
Any help is appreciated! Thanks! If you need any more information I'll be happy to provide it.
Sincerely,
tf.rz
EDIT: I am using Visual Studio C# 2008. .NET 3.5 SP1
The basic premise is related to another question i have posted regarding the reordering of datatable columns. But to shorten the explanation. I only need to say that I just simply need the names of the elements that the xsd schema will validate (in the proper order). I have a few xsd schemas, all of which follow the same "format" and are very, very static files. Thus, I know that I can safely look for the sequence tag and get all its child nodes. While Michael mentioned how there are many ways to write a schema, the schemas that I am working with are all similar and static in those regards, so if I am able to do this, it will work 100% of the time. =)
回答1:
Your basic problem is that you need to understand how to use XPath to access a source document that uses namespaces. This is pretty elementary stuff and there are a thousand answers on this forum that explain it. You need a prefixed name - xs:sequence - and you need to tell your XPath engine that the "xs" prefix represents the URI "http://www.w3.org/2001/XMLSchema" - the incantation for this varies from one XPath engine to another, and I'm afraid I don't recognize the class names in your code so I don't know which one it is.
I think there is a deeper problem however. Extracting information from a source XSD document using XPath expressions is a bit like trying to extract information from Java programs using a regular expression. It will work some of the time; if you're clever you can make it work quite a lot of the time; but it will never work all of the time, because there are too many ways of writing a schema. A much better idea is to process the schema using a real schema processor, and then use its API to ask questions about the contents of the schema.
来源:https://stackoverflow.com/questions/6524157/selecting-nodes-in-xsd-schema-using-xpath