Пример #1
0
        public void GetElementAttributeNumberOfXmlNode()
        {
            string projectFileContents = @"
                <Project xmlns=`msbuild`>

                  <PropertyGroup Condition=`false`>
                    <Optimize>true</Optimize>
                    <WarningLevel>
                       4
                    </WarningLevel>
                    <DebugType/>
                    goo
                    <OutputPath/>
                  </PropertyGroup>
                  <ItemGroup>
                    <Compile Include=`a.cs` Condition=`true`/>
                    <Reference Include=`msbuildengine`>
                       <HintPath>c:\\msbengine.dll</HintPath>
                    </Reference>
                  </ItemGroup>

                </Project>
                ";

            projectFileContents = projectFileContents.Replace("`", "\"");
            XmlDocument doc = new XmlDocument();

            doc.LoadXml(projectFileContents);

            int elementNumber;
            int attributeNumber;

            // Find the element/attribute number of the <Optimize/> node.
            Assertion.Assert(XmlSearcher.GetElementAndAttributeNumber(
                                 //  <Project>  <PropertyGroup> <Optimize>
                                 doc.FirstChild.ChildNodes[0].ChildNodes[0],
                                 out elementNumber,
                                 out attributeNumber));
            Assertion.AssertEquals(3, elementNumber);
            Assertion.AssertEquals(0, attributeNumber);

            // Find the element/attribute number of the "4" inside the <WarningLevel/> tag.
            Assertion.Assert(XmlSearcher.GetElementAndAttributeNumber(
                                 //  <Project>  <PropertyGroup> <WarningLevel>  4
                                 doc.FirstChild.ChildNodes[0].ChildNodes[1].ChildNodes[0],
                                 out elementNumber,
                                 out attributeNumber));
            Assertion.AssertEquals(6, elementNumber);
            Assertion.AssertEquals(0, attributeNumber);

            // Find the element/attribute number of the <DebugType/> node.
            Assertion.Assert(XmlSearcher.GetElementAndAttributeNumber(
                                 //  <Project>  <PropertyGroup> <DebugType>
                                 doc.FirstChild.ChildNodes[0].ChildNodes[2],
                                 out elementNumber,
                                 out attributeNumber));
            Assertion.AssertEquals(7, elementNumber);
            Assertion.AssertEquals(0, attributeNumber);

            // Find the element/attribute number of the "goo" node.
            Assertion.Assert(XmlSearcher.GetElementAndAttributeNumber(
                                 //  <Project>  <PropertyGroup> goo
                                 doc.FirstChild.ChildNodes[0].ChildNodes[3],
                                 out elementNumber,
                                 out attributeNumber));
            Assertion.AssertEquals(8, elementNumber);
            Assertion.AssertEquals(0, attributeNumber);

            // Find the element/attribute number of the <Reference> node.
            Assertion.Assert(XmlSearcher.GetElementAndAttributeNumber(
                                 //  <Project>  <ItemGroup>   <Reference>
                                 doc.FirstChild.ChildNodes[1].ChildNodes[1],
                                 out elementNumber,
                                 out attributeNumber));
            Assertion.AssertEquals(12, elementNumber);
            Assertion.AssertEquals(0, attributeNumber);

            // Find the element/attribute number of the "Condition" attribute on the <Compile> node.
            Assertion.Assert(XmlSearcher.GetElementAndAttributeNumber(
                                 //  <Project>  <ItemGroup>   <Compile>     Condition
                                 doc.FirstChild.ChildNodes[1].ChildNodes[0].Attributes[1],
                                 out elementNumber,
                                 out attributeNumber));
            Assertion.AssertEquals(11, elementNumber);
            Assertion.AssertEquals(2, attributeNumber);

            // Try passing in an Xml element that doesn't even exist in the above document.
            // This should fail.
            Assertion.Assert(!XmlSearcher.GetElementAndAttributeNumber(
                                 (new XmlDocument()).CreateElement("Project"),
                                 out elementNumber,
                                 out attributeNumber));
        }
 private static void Main()
 {
     // This particular project is of really low quality, but here i really didn't have time :)
     var xmlSearcher = new XmlSearcher(new CarsDbContext(), new ConsoleLogger());
 }