如何在C#中查找XSD根元素
美好的一天。
我所知。 XML文件中有一个根元素。
但是从XSD文件结构来看,获取根元素值并不容易。 有没有办法做到这一点?
(我不想在我的项目中使用硬代码来查找XSD根元素值。我想找到“RootValueHere”的根元素
谢谢。
虽然单个文档只能包含一个根元素,但XSD实际上可以定义多个有效的根元素 。
如果您只想真正希望单个类型作为根元素有效,那么它应该是唯一被引用为
。
例如,在上面的模式中,DocumentInfo和Prerequisite节点也是有效的根元素。 要将架构限制为只有一个有效的根节点,请将DocumentInfo和Prerequisite元素替换为简单的complexType定义:
... ....
更新:要访问元素的名称,您只需要查看XmlElement上的Name属性:
XmlDocument doc = new XmlDocument(); doc.Load("D:\\schema.xsd"); // Load the document from the root of an ASP.Net website XmlElement schemaElement = doc.DocumentElement; // The root element of the schema document is the element string elementName = schemaElement.LocalName; // This will print "schema" foreach (XmlNode ele in schemaElement.ChildNodes) { if (ele.LocalName == "element") { // This is a valid root node // Note that there will be *more than one* of these if you have multiple elements declare at the base level } }
我相信
XmlDocument myDocument = new XmlDocument("my.xml"); myDocument.DocumentElement(); //gets root document node