优化在c#web服务中读取XML

时间:2012-04-16 17:55:42

标签: c# asp.net xml

我正在尝试为我的网络服务找到优化以下代码的方法

在我的webservice中,我收到10个不同的XML文件,每个xml文件总是相同的格式,目前我有10种不同的方法来处理每一个,下面的代码是其中一个方法的一个例子。

方法之间的唯一区别是局部变量和节点属性。

我希望只有一种方法,而不是10种方法几乎相同。

有人可以就实现这一目标的最佳途径提出建议吗?

public void NavigateXmlSessionData(XPathNavigator xPathNav)
{
    xPathNav.MoveToRoot();
    xPathNav.MoveToFirstChild();

    xPathNav.MoveToFirstChild();
    string  description, personId;

   // initalise vars
    description = "";
    personId = "";

    do
    {
        //display the child nodes
        if (xPathNav.MoveToFirstChild())
        {
            while (xPathNav.MoveToNext())
            {
                switch (xPathNav.Name)
                {
                    #region NodeAttributes
                    case "Description":
                        {
                            StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200);
                            description = strBuild.Replace('\'', ' ').ToString();
                            break;
                        }
                    case "PersonId":
                        {
                            StringBuilder strBuild = new StringBuilder(xPathNav.Value, 200);
                            personId = strBuild.Replace('\'', ' ').ToString();
                            break;
                        }
                        #endregion
                }
            }
            //move back to the parent
            xPathNav.MoveToParent();
        }

        ProcessRecord();
        // initalise vars
        description = "";
        personId = "";

    } while (xPathNav.MoveToNext());
}

0 个答案:

没有答案