使用C#和XMLDOM提取xml数据
本文关键字:xml 数据 提取 XMLDOM 使用 | 更新日期: 2023-09-27 18:00:27
我有一个xml,它看起来像这样:
<dfs:dataFields>
<d:REQUIREMENT_SPECIFICATION ProjectName="Test 1" ProjectWorkCode="909"
FunctionDepartmentName="X department" BrandApplicableName="All" ProjectManagerName=""
ProjectSponserName="" BackgroundDescription="others and users use the Online tool to
to add users"
StepChangeGoalDescription="In 2011, the new service will be active" ServiceImpactedName="xy service"
xdado:OJsZDA="0">
</d:REQUIREMENT_SPECIFICATION>
</dfs:dataFields>
我需要提取引号中的数据。例如,我希望它打印出来:需求说明书项目名称:试验1项目工作代码:909职能部门名称:X部门……等等…
我正在使用以下代码。它正在打印d:REQUIMENT_SPECIFATION和dfs:dataFields,但不会打印其他任何内容。
XPathNavigator nav;
XPathDocument docNav;
docNav = new XPathDocument("test.xml");
nav = docNav.CreateNavigator();
nav.MoveToRoot();
//Move to the first child node (comment field).
nav.MoveToFirstChild();
do
{
//Find the first element.
if (nav.NodeType == XPathNodeType.Element)
{
//Determine whether children exist.
if (nav.HasChildren == true)
{
//Move to the first child.
nav.MoveToFirstChild();
Console.WriteLine(nav.Name);
Console.WriteLine(nav.Value);
//Loop through all of the children.
do
{
//Display the data.
nav.MoveToFirstChild();
Console.WriteLine(nav.Name);
Console.WriteLine(nav.Value);
} while (nav.MoveToNext());
}
}
} while (nav.MoveToNext());
//Pause.
Console.ReadLine();
你能告诉我正确的方向吗?
对于这种情况,我更喜欢使用XmlDocument。您可以定义在文档中加载xml并只返回根节点的方法,在主方法中只循环节点的属性:
private void ProcessAndDumpXml()
{
StreamReader xmlStream = new StreamReader("example1.xml");
XmlNode root = GetRootNode(xmlStream);
// process nodes
// ...
}
private XmlNode GetRootNode(StreamReader streamReader)
{
XmlDocument xmlDocument = new XmlDocument();
XmlNamespaceManager nsmgr = new XmlNamespaceManager(new NameTable());
nsmgr.AddNamespace("dfs", "schema1");
nsmgr.AddNamespace("d", "schema1");
nsmgr.AddNamespace("xdado", "schema1");
XmlParserContext context = new XmlParserContext(null, nsmgr, null, XmlSpace.None);
XmlReaderSettings xset = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment };
XmlReader rd = XmlReader.Create(streamReader, xset, context);
xmlDocument.Load(rd);
return xmlDocument.DocumentElement.FirstChild;
}
除了循环子元素外,还需要循环每个元素的属性。
您发布的文档不是有效的XML文档,因为它缺少命名空间规范。但是,假设存在名称空间,您可以使用LINQ to XML来实现,如下所示:
var doc= XDocument.Load(xmlFile);
XNamespace dNs = "http://actual-d-namespace-uri";
foreach(var element in doc.Root.Elements(dNs + "REQUIREMENT_SPECIFICATION"))
{
var attributes = element.Attributes()
.Select(a => string.Format("{0}: {1}", a.Name, a.Value));
Console.WriteLine("Requirement Specification " + string.Join(" ", attributes));
}