c# - C# 中的 XSD 元素

标签 c#

我有一个 XSD 文件,我需要获取其中所有元素的名称列表。

我尝试了以下方法:

openFileDialog1.ShowDialog();
tbSchema.Text = openFileDialog1.FileName;

cbElement.Items.Clear();

XmlSchemaSet schemaSet = new XmlSchemaSet();
schemaSet.Add("", tbSchema.Text);
schemaSet.Compile();
XmlSchema customerSchema = null;
foreach (XmlSchema schema in schemaSet.Schemas())
{
    customerSchema = schema;
}
foreach (XmlSchemaElement element in customerSchema.Elements.Values)
{
    cbElement.Items.Add(element.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 <schema> element
 string elementName = schemaElement.LocalName;    // This will print "schema"
 foreach (XmlNode ele in schemaElement.ChildNodes)
 {
  if (ele.LocalName == "element")
  {
     .....
  }
 }

关于c# - C# 中的 XSD 元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13550722/

相关文章:

javascript - 检查参数的更好方法?

C# net core巧妙简化Lambda表达式 - 更新

c# - 从 appdomain 中解包一个不可序列化的类

c# - 将文本添加到视频中并保存输出

c# - 验证长类型输入

javascript - 有选择地支持某些浏览器

c# - 如何使用 MSBuild 将链接文件添加到 csproj 文件。 (3.5 框架)

c# - .NET MAUI - 为什么绑定(bind)不能与 ObservableCollection 一起使用?

c# - 将字节数组作为 char* 从 C# 传递到 C++ DLL

c# - 如何检测 jpeg 是否包含 cmyk 颜色配置文件?