使用C#和XML DOM提取xml数据

时间:2011-06-27 18:36:11

标签: c# xml dom xml-parsing

我有一个看起来像这样的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&#xA;to add users" 
StepChangeGoalDescription="In 2011, the new service will be active" ServiceImpactedName="xy service" 
xdado:OJsZDA="0">
</d:REQUIREMENT_SPECIFICATION>
</dfs:dataFields>

我需要提取引号中的数据。例如,我想要打印出来: 要求规格 项目名称:测试1 ProjectWorkCode:909 FunctionDepartmentName:X部门 ......等等......

我正在使用以下代码。它打印出d:REQUIREMENT_SPECIFICATION和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();
你能指出我正确的方向吗?

3 个答案:

答案 0 :(得分:1)

我更喜欢在这种情况下使用XmlDocument。您可以定义在文档中加载xml并返回根节点的方法,并在main方法中循环遍历Node的属性:

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;
}

答案 1 :(得分:0)

除了遍历子元素之外,还需要循环遍历每个元素的属性。

答案 2 :(得分:0)

您发布的文档不是有效的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));
}