1

我尝试从 xml 配置中获取一些特定的值。请参见下面的示例。

<?xml version="1.0" encoding="utf-8"?>
<ExcelConfig>
    <ExcelDocument name="Customer" type="flat">
        <IdentityColumn>
            <Column name="Id" />
        </IdentityColumn>
        <Validate>
            <Column name="Name" mandatory="true" />
            <Column name="FirstName" mandatory="true" />
            <OrColumns mandatory="true">
                <Column name="PostalCode" mandatory="false" />
                <Column name="PostalCode2" mandatory="false" />
            </OrColumns>
        </Validate>
    </ExcelDocument>
    <ExcelDocument name="Company" type="flat">
        <IdentityColumn>
            <Column name="Id" />
        </IdentityColumn>
        <Validate>
            <Column name="Name" mandatory="true" />
            <Column name="FirstName" mandatory="true" />
            <OrColumns mandatory="true">
                <Column name="PostalCode" mandatory="false" />
                <Column name="PostalCode2" mandatory="false" />
            </OrColumns>
        </Validate>
    </ExcelDocument>
    <ExcelDocument name="SomeOtherType" type="block">
        <IdentityBlock>
            <Column name="Period" col="A" />
            <Column name="Period2" col="B" />
        </IdentityBlock>
        <Validate>
            <Column name="Name" mandatory="true" />
            <Column name="FirstName" mandatory="true" />
        </Validate>
    </ExcelDocument>
</ExcelConfig>

我使用以下代码从 excel 文件中获取一些信息。“ValidationConfiguration”是具有先前配置的字符串。

            //Get Different NodeTypes of Excel documents
            List<XPathNavigator> types = XmlHelper.GetNodeTypes(validationConfiguration, "/ExcelConfig/ExcelDocument");
            List<XPathNavigator> flatTypes = XmlHelper.GetNodeTypes(validationConfiguration,
                                                                     "//ExcelConfig/ExcelDocument[@type='flat']");
            List<XPathNavigator> blockTypes = XmlHelper.GetNodeTypes(validationConfiguration,
                                                                     "//ExcelConfig/ExcelDocument[@type='block']");


            //First we check if the file is from the flat type and get the IdentityColumns
            List<XPathNavigator> identityColumnsNode = XmlHelper.GetNodeTypes(validationConfiguration, "//ExcelConfig/ExcelDocument[@type='flat']/IdentityColumn");

您可以在下面找到 XmlHelper 类。

 public static class XmlHelper
    {
        public static List<XPathNavigator> GetNodeTypes(string xmlConfiguration,string xPath)
        {
            XPathDocument doc = new XPathDocument(new StringReader(xmlConfiguration));
            XPathNavigator nav = doc.CreateNavigator();

            XPathExpression expr = nav.Compile(xPath);
            List<XPathNavigator> elements = new List<XPathNavigator>();

            foreach (XPathNavigator node in nav.Select(expr))
            {
                elements.Add(node);   
            }

            return elements;
        }

        public static List<string> GetIdentityColumnNames(List<XPathNavigator> xPathNavigators)
        {
            List<string> identityColumns = new List<string>();

            foreach (XPathNavigator xPathNavigator in xPathNavigators)
            {
                foreach (XPathNavigator test in xPathNavigator.Select("//Column"))
                {
                    identityColumns.Add(test.GetAttribute("name", ""));
                }
            }

            return identityColumns;
        }
    }

现在我想做以下事情。我选择了identityColumnsNodes(它们包含来自具有平面类型的exceldocuments的IdentityColumn)。我得到的所有类型的列。但是当我尝试这样做时,我会从整个文件中取回所有列。他不只是我使用的节点中的项目。

 foreach (XPathNavigator identityColumNode in identityColumnsNode)
                        {
                            List<string> identityColumns = XmlHelper.GetIdentityColumnNames(identityColumnsNode);          
                        }

第二个问题/我想做的事情->从特定文件中选择正确的验证节点的最佳方法。有了identityColumns(我回来了,我的HeaderRow Cells列表我知道它是什么文件。但是我怎样才能选择那个验证节点呢?

还是他们有更好的方法来做这些事情?

4

0 回答 0