如何在 C# 中使用 xml 解析器提取 xml 中节点的所有值?
How to extract all values of a node in xml using xml parser in C#?
我正在尝试使用 C#
解析 visual studio 中的 XML
文件,并希望存储每个节点的 attributes
(单独供进一步使用)。 XML
文件如下所示:
<FBType GUID="123" Name="plcStart" Comment="Composite Function Block Type" Namespace="abc">
<Attribute Name="Configuration.FB.IDCounter" Value="1" />
<Identification Standard="123" />
<VersionInfo Organization="org" Version="0.0" Author="sb" Date="8/23/2013" Remarks="template" />
<InterfaceList>
<EventInputs>
<Event Name="ACK" />
</EventInputs>
<EventOutputs>
<Event Name="START" />
</EventOutputs>
</InterfaceList>
</FBType>
我写了下面的代码,但我只检索了后三个节点及其 attributes
。我尝试了现在注释的代码部分,但是没有用。
如有任何帮助,我将不胜感激。
try
{
foreach (XmlNode node in doc.DocumentElement)
{
//foreach (XmlNode child in node.ChildNodes)
//{
for (int i = 0; i < node.Attributes.Count; i++)
{
string atr = node.Attributes[i].InnerText;
Console.WriteLine(node.Name + " " + atr);
}
//}
foreach (XmlNode child in node.ChildNodes)
{
for (int j = 0; j < node.ChildNodes.Count; j++)
{
string childName = child.Attributes[j].InnerText;
Console.WriteLine(childName);
}
}
}
}
catch (Exception e) { }
使用 Xml Linq,您可以将结果展平到 class 对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication152
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<FBTType> fBTTypes = doc.Descendants("FBType").Select(x => new FBTType()
{
guid = (string)x.Attribute("GUID"),
typeName = (string)x.Attribute("Name"),
comment = (string)x.Attribute("Comment"),
ns = (string)x.Attribute("Namespace"),
name = (string)x.Element("Attribute").Attribute("Name"),
value = (string)x.Element("Attribute").Attribute("Value"),
identification = (string)x.Element("Identification").Attribute("Standard"),
organization = (string)x.Element("VersionInfo").Attribute("Organization"),
version = (string)x.Element("VersionInfo").Attribute("Version"),
author = (string)x.Element("VersionInfo").Attribute("Author"),
date = (DateTime)x.Element("VersionInfo").Attribute("Date"),
remarks = (string)x.Element("VersionInfo").Attribute("Remarks"),
eventInputs = x.Descendants("EventInputs").FirstOrDefault().Elements("Event").Select(y => (string)y.Attribute("Name")).ToArray(),
eventOutputs = x.Descendants("EventOutputs").FirstOrDefault().Elements("Event").Select(y => (string)y.Attribute("Name")).ToArray(),
}).ToList();
foreach (FBTType fbTType in fBTTypes)
{
fbTType.print();
}
Console.ReadLine();
}
}
public class FBTType
{
public string guid { get; set; }
public string typeName { get; set; }
public string comment { get; set; }
public string ns { get; set; }
public string name { get; set; }
public string value { get; set; }
public string identification { get; set; }
public string organization { get; set; }
public string version { get; set; }
public string author { get; set; }
public DateTime date { get; set; }
public string remarks { get; set; }
public string[] eventInputs { get; set; }
public string[] eventOutputs { get; set; }
public void print()
{
Console.WriteLine("GUID : '{0}'",guid);
Console.WriteLine("Type Name : '{0}'",typeName);
Console.WriteLine("Comment : '{0}'",comment);
Console.WriteLine("Namespace : '{0}'",ns);
Console.WriteLine("Name : '{0}'",name);
Console.WriteLine("Value : '{0}'",value);
Console.WriteLine("Identification : '{0}'",identification);
Console.WriteLine("Organization : '{0}'",organization);
Console.WriteLine("Version : '{0}'",version);
Console.WriteLine("Author: '{0}'",author);
Console.WriteLine("Date : '{0}'",date.ToString());
Console.WriteLine("Remarks : '{0}'",remarks);
Console.WriteLine("Event Inputs : '{0}'",string.Join(",", eventInputs));
Console.WriteLine("Envent Outputs : '{0}'", string.Join(",", eventOutputs));
}
}
}
我正在尝试使用 C#
解析 visual studio 中的 XML
文件,并希望存储每个节点的 attributes
(单独供进一步使用)。 XML
文件如下所示:
<FBType GUID="123" Name="plcStart" Comment="Composite Function Block Type" Namespace="abc">
<Attribute Name="Configuration.FB.IDCounter" Value="1" />
<Identification Standard="123" />
<VersionInfo Organization="org" Version="0.0" Author="sb" Date="8/23/2013" Remarks="template" />
<InterfaceList>
<EventInputs>
<Event Name="ACK" />
</EventInputs>
<EventOutputs>
<Event Name="START" />
</EventOutputs>
</InterfaceList>
</FBType>
我写了下面的代码,但我只检索了后三个节点及其 attributes
。我尝试了现在注释的代码部分,但是没有用。
如有任何帮助,我将不胜感激。
try
{
foreach (XmlNode node in doc.DocumentElement)
{
//foreach (XmlNode child in node.ChildNodes)
//{
for (int i = 0; i < node.Attributes.Count; i++)
{
string atr = node.Attributes[i].InnerText;
Console.WriteLine(node.Name + " " + atr);
}
//}
foreach (XmlNode child in node.ChildNodes)
{
for (int j = 0; j < node.ChildNodes.Count; j++)
{
string childName = child.Attributes[j].InnerText;
Console.WriteLine(childName);
}
}
}
}
catch (Exception e) { }
使用 Xml Linq,您可以将结果展平到 class 对象
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication152
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
List<FBTType> fBTTypes = doc.Descendants("FBType").Select(x => new FBTType()
{
guid = (string)x.Attribute("GUID"),
typeName = (string)x.Attribute("Name"),
comment = (string)x.Attribute("Comment"),
ns = (string)x.Attribute("Namespace"),
name = (string)x.Element("Attribute").Attribute("Name"),
value = (string)x.Element("Attribute").Attribute("Value"),
identification = (string)x.Element("Identification").Attribute("Standard"),
organization = (string)x.Element("VersionInfo").Attribute("Organization"),
version = (string)x.Element("VersionInfo").Attribute("Version"),
author = (string)x.Element("VersionInfo").Attribute("Author"),
date = (DateTime)x.Element("VersionInfo").Attribute("Date"),
remarks = (string)x.Element("VersionInfo").Attribute("Remarks"),
eventInputs = x.Descendants("EventInputs").FirstOrDefault().Elements("Event").Select(y => (string)y.Attribute("Name")).ToArray(),
eventOutputs = x.Descendants("EventOutputs").FirstOrDefault().Elements("Event").Select(y => (string)y.Attribute("Name")).ToArray(),
}).ToList();
foreach (FBTType fbTType in fBTTypes)
{
fbTType.print();
}
Console.ReadLine();
}
}
public class FBTType
{
public string guid { get; set; }
public string typeName { get; set; }
public string comment { get; set; }
public string ns { get; set; }
public string name { get; set; }
public string value { get; set; }
public string identification { get; set; }
public string organization { get; set; }
public string version { get; set; }
public string author { get; set; }
public DateTime date { get; set; }
public string remarks { get; set; }
public string[] eventInputs { get; set; }
public string[] eventOutputs { get; set; }
public void print()
{
Console.WriteLine("GUID : '{0}'",guid);
Console.WriteLine("Type Name : '{0}'",typeName);
Console.WriteLine("Comment : '{0}'",comment);
Console.WriteLine("Namespace : '{0}'",ns);
Console.WriteLine("Name : '{0}'",name);
Console.WriteLine("Value : '{0}'",value);
Console.WriteLine("Identification : '{0}'",identification);
Console.WriteLine("Organization : '{0}'",organization);
Console.WriteLine("Version : '{0}'",version);
Console.WriteLine("Author: '{0}'",author);
Console.WriteLine("Date : '{0}'",date.ToString());
Console.WriteLine("Remarks : '{0}'",remarks);
Console.WriteLine("Event Inputs : '{0}'",string.Join(",", eventInputs));
Console.WriteLine("Envent Outputs : '{0}'", string.Join(",", eventOutputs));
}
}
}