在 XML/XSD 模式文件中,我们如何从元素中提取(解析)某些信息?
In an XML/XSD schema file, how can we extract (parse) certain information from an element?
有没有办法从 XML 文件中解析某些数据,并将该信息输出到 excel(csv) 文件中?
使用此代码。您需要将 xmldocument 转换为 xdocument。因此您可以轻松捕获每个元素及其数据。我使用了您提供的相同文件。我还提到了如何读取循环及其子元素中的元素的示例。
class Program
{
static void Main(string[] args)
{
Parse();
}
public static void Parse()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\New Text Document.xsd");
var captureElements = new List<CustomElements>();
var xdocument = xmlDoc.ToXDocument();
foreach (var element in xdocument.Elements())
{
foreach (var node in element.Elements()) //childs...
{
if (node.Name.LocalName.Equals("ElementType"))
{
foreach (var scopeNode in node.Elements())
{
if (scopeNode.Name.LocalName.Equals("element"))
{
var xml = XElement.Parse(scopeNode.ToString());
var customElement = new CustomElements();
customElement.Type = xml.Attribute("type")?.Value;
customElement.Label = xml.Attribute("label")?.Value;
customElement.CompTypes = xml.Attribute("CompTypes")?.Value;
customElement.Readonly = xml.Attribute("readonly")?.Value;
customElement.Hidden = xml.Attribute("hidden")?.Value;
customElement.Require = xml.Attribute("require")?.Value;
captureElements.Add(customElement);
}
}
}
}
}
}
}
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using (var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
}
public class CustomElements
{
public string Type { get; set; }
public string Label { get; set; }
public string CompTypes { get; set; }
public string Readonly { get; set; }
public string Hidden { get; set; }
public string Require { get; set; }
}
在 XSLT 中很容易完成。您不需要架构。除非有特殊字符需要转义等,就这么简单:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" expand-text="yes">
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="text"/>
<xsl:template match="Element"
>{@type},{@label},{@CompTypes},{@readonly},{@hidden},{@required}
</xsl:template>
</xsl:transform>
这是一个 XSLT 3.0 解决方案;如果您更喜欢使用与 .NET 捆绑在一起的 XSLT 1.0 处理器,那么它会更冗长但仍然非常简单。我没有包含 header 行,但添加它很简单。
在 XSLT 3.0 中,您甚至可以添加自动化以将此应用到整个 collection 个 XML 文件(在 1.0 中,您需要在调用脚本中执行此操作)。
在 C# 中,使用 System.Xml.XmlDocument,在 SelectNodes 中使用 XPath 语法
XmlDocument xml = new XmlDocument();
xml.Load( strFile );
foreach (XmlElement ndRow in xml.SelectNodes("//element")) {
string strType = ndRow.GetAttribute("type");
string strLabel = ndRow.GetAttribute("label");
}
有没有办法从 XML 文件中解析某些数据,并将该信息输出到 excel(csv) 文件中?
使用此代码。您需要将 xmldocument 转换为 xdocument。因此您可以轻松捕获每个元素及其数据。我使用了您提供的相同文件。我还提到了如何读取循环及其子元素中的元素的示例。
class Program
{
static void Main(string[] args)
{
Parse();
}
public static void Parse()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"D:\New Text Document.xsd");
var captureElements = new List<CustomElements>();
var xdocument = xmlDoc.ToXDocument();
foreach (var element in xdocument.Elements())
{
foreach (var node in element.Elements()) //childs...
{
if (node.Name.LocalName.Equals("ElementType"))
{
foreach (var scopeNode in node.Elements())
{
if (scopeNode.Name.LocalName.Equals("element"))
{
var xml = XElement.Parse(scopeNode.ToString());
var customElement = new CustomElements();
customElement.Type = xml.Attribute("type")?.Value;
customElement.Label = xml.Attribute("label")?.Value;
customElement.CompTypes = xml.Attribute("CompTypes")?.Value;
customElement.Readonly = xml.Attribute("readonly")?.Value;
customElement.Hidden = xml.Attribute("hidden")?.Value;
customElement.Require = xml.Attribute("require")?.Value;
captureElements.Add(customElement);
}
}
}
}
}
}
}
public static class DocumentExtensions
{
public static XmlDocument ToXmlDocument(this XDocument xDocument)
{
var xmlDocument = new XmlDocument();
using (var xmlReader = xDocument.CreateReader())
{
xmlDocument.Load(xmlReader);
}
return xmlDocument;
}
public static XDocument ToXDocument(this XmlDocument xmlDocument)
{
using (var nodeReader = new XmlNodeReader(xmlDocument))
{
nodeReader.MoveToContent();
return XDocument.Load(nodeReader);
}
}
}
public class CustomElements
{
public string Type { get; set; }
public string Label { get; set; }
public string CompTypes { get; set; }
public string Readonly { get; set; }
public string Hidden { get; set; }
public string Require { get; set; }
}
在 XSLT 中很容易完成。您不需要架构。除非有特殊字符需要转义等,就这么简单:
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="3.0" expand-text="yes">
<xsl:mode on-no-match="shallow-skip"/>
<xsl:output method="text"/>
<xsl:template match="Element"
>{@type},{@label},{@CompTypes},{@readonly},{@hidden},{@required}
</xsl:template>
</xsl:transform>
这是一个 XSLT 3.0 解决方案;如果您更喜欢使用与 .NET 捆绑在一起的 XSLT 1.0 处理器,那么它会更冗长但仍然非常简单。我没有包含 header 行,但添加它很简单。
在 XSLT 3.0 中,您甚至可以添加自动化以将此应用到整个 collection 个 XML 文件(在 1.0 中,您需要在调用脚本中执行此操作)。
在 C# 中,使用 System.Xml.XmlDocument,在 SelectNodes 中使用 XPath 语法
XmlDocument xml = new XmlDocument();
xml.Load( strFile );
foreach (XmlElement ndRow in xml.SelectNodes("//element")) {
string strType = ndRow.GetAttribute("type");
string strLabel = ndRow.GetAttribute("label");
}