1

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?

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you soo much. this fixed my problem. I am looping through all attributes and printing out attribute.Item(i).Name and attribute.Item(i).Value; Thanks for your help on this!
Do not forget to add null checks, and another validation constructions, good luck!
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.