本文介紹了C# - 解析 XSD 架構 - 將所有元素獲取到組合框的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我有 XSD 架構文件,我需要用架構文件中的元素填充我的組合框...
I have XSD Schema file and i need to fill my combobox with the elements from the schema file...
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="auto">
<xs:complexType>
<xs:sequence>
<!-- Znacka -->
<xs:element name="znacka" type="xs:string"/>
<!-- pocetOsob -->
<xs:element name="pocetOsob" type="xs:int"/>
<!-- maxRychlost -->
<xs:element name="maxRychlost">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="jednotka" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
<!-- Motor -->
<xs:element name="motor">
<xs:complexType>
<xs:sequence>
<xs:element name="vykon">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:decimal">
<xs:attribute name="jednotka" type="xs:string"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="vyrobni_cislo" type="xs:string"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
有人知道怎么做嗎?通過xpath?我有一半工作的代碼...我收到一條帶有元素 auto 的消息.
Anyone had idea how to do it? Through xpath? I have a half working code... I got a message with element auto.
String path = openSchema.FileName;
XmlTextReader xsd_file = new XmlTextReader(path);
XmlSchema schema = new XmlSchema();
schema = XmlSchema.Read(xsd_file, null);
MessageBox.Show(schema.Items.Count.ToString());
foreach (XmlSchemaElement element in schema.Items)
{
elements.Items.Add(element.Name);
MessageBox.Show(element.Name);
}
非常感謝!
推薦答案
string xml = <your xml>;
var xs = XNamespace.Get("http://www.w3.org/2001/XMLSchema");
var doc = XDocument.Parse(xml);
// if you have a file: var doc = XDocument.Load(<path to xml file>)
foreach(var element in doc.Descendants(xs + "element"))
{
Console.WriteLine(element.Attribute("name").Value);
}
// outputs:
// auto
// znacka
// pocetOsob
// maxRychlost
// motor
// vykon
這篇關于C# - 解析 XSD 架構 - 將所有元素獲取到組合框的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!