gkaur3
gkaur3

Reputation: 11

extract xml data using C# and XML DOM

I have an xml which looks like this:

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

I need to extract the data which is in the quotes. For example, I want it to print out: Requirement Specification Project Name: Test 1 ProjectWorkCode: 909 FunctionDepartmentName: X department ...... and so on...

I am using the following code. it's printing out d:REQUIREMENT_SPECIFICATION and dfs:dataFields but won't print anything else.

        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();

Can you please point me in the right direction?

Upvotes: 1

Views: 7517

Answers (3)

svick
svick

Reputation: 244757

The document you posted is not a valid XML document, because it lacks namespace specifications. But assuming the namespaces were present, you could do it using LINQ to XML like this:

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

Upvotes: 0

sll
sll

Reputation: 62484

I prefer to use XmlDocument for such cases. You can define method which loads xml in the document and just return root node, and in the main method just loop through the Node's attributes:

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

Upvotes: 1

Ethan Cabiac
Ethan Cabiac

Reputation: 4993

In addition to looping through the children, you also need to loop through the attributes of each element.

Upvotes: 0

Related Questions