简体   繁体   中英

How to get xml element using XML Predicate<XmlNode>?

I have the following lines

XmlNode controlNode = comparison.ControlDetails.Target;
XmlAttribute attr = controlNode as XmlAttribute; //I believe this line needs changing.

My goal is to get XML Node, [0] {Element, Name= “creationTimestamp”}, I'm not sure how to get this XML node and would appreciate some help, please let me know if you want more information, I'm just really stuck on this and have been for days, please help me get the above element, I've added my method to.

debugging

Below is my ElementDifferenceEvaluatorTest.cs class with the xml and diff builder.

[TestFixture]
    public class ElementDifferenceEvaluatorTest
    {
        [Test]
        public void TestUserguideExample()
        {   string xmla = @"<sbl xmlns:fpml='www.test.co.uk'>
            <creationTimestamp>2018-09-12T17:33:06+01:00</creationTimestamp>
            <messageVersion>2.0</messageVersion>
            <publishedVersions>2.0</publishedVersions>
            <waqeBundle>
            <reference>123456</reference>
            <firstReference>1234567</firstReference>
            <version>15151515</version>
            <type>The-Buumi</type>
            <action>Yes</action>
            <subAction>new_sumie</subAction>
            </waqeBundle>
            <sourceEnvironment>UAT2</sourceEnvironment>
            </sbl>";

            string xmlb =
            @"<sbl xmlns:fpml='www.test.co.uk'>
            <messageVersion>2.0</messageVersion>
            <publishedVersions>2.0</publishedVersions>
            <waqeBundle>
            <reference>123456</reference>
            <firstReference>1234567</firstReference>
            <version>15151515</version>
            <type>The-Buumi</type>
            <action>Yes</action>
            <subAction>new_sumie</subAction>
            </waqeBundle>
            <sourceEnvironment>UAT2</sourceEnvironment>
            </sbl>";

            var myDiff = DiffBuilder.Compare(xmla).WithTest(xmlb)
                .WithDifferenceEvaluator(new ElementDifferenceEvaluator("creationTimestamp").Evaluate)
                .IgnoreComments()
                .CheckForSimilar()
                .Build();

            Assert.IsFalse(myDiff.HasDifferences(), myDiff.ToString());
        }
    }
}

Next is my ElementDifferenceEvaluator.cs

public class ElementDifferenceEvaluator
    {
        private string attributeName;

        public ElementDifferenceEvaluator(string attributeName)

        {
            this.attributeName = attributeName;

        }

        public ComparisonResult Evaluate(Comparison comparison, ComparisonResult outcome)

        {
            if (outcome == ComparisonResult.EQUAL || outcome == ComparisonResult.SIMILAR)
                return outcome; // only evaluate differences. 

            XmlNode controlNode = comparison.ControlDetails.Target; //why is this null?
            XmlAttribute attr = controlNode as XmlAttribute; //Why is this null?

            if (attr != null)

            {
                if (attr.Name == attributeName)
                {
                    return ComparisonResult.SIMILAR; // will evaluate this difference as similar 

                }
            }
            return outcome;
        } 
     }
}

I assume you want to get the node <creationTimeStamp/> ? If so-I would do it like this:

XmlDocument xdoc = new XmlDocument();
doc.LoadXml("your XML as a string");  //as an alternative, check out doc.Load()
XmlNodeList aNodes = xdoc.GetElementsByTagName("creationTimeStamp");

This contains all tags by the Name creationTimeStamp, you could use aNodes[0] for the first item. Now, you have the possibility to look for attributes or other stuff, I mostly use .InnerXml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM