通过C#读取和编辑XML文件中的特定XML代码

时间:2018-07-30 15:40:09

标签: c# xml parsing

我正在尝试用C#创建一个程序,该程序将打开Main.xml文件中提到的所有XML文件,查找某些名称标签,并在其中添加后缀(blabla_name,{ {1}},Blabla_unit_nameLocation_code)。问题在于,名称是关系属性,其名称取决于所使用的文件而具有不同的标记。例如,WO_Name文件中的名称可能是<Structure_name>,但可能是structure.xml<From_structure_name>文件中。

因此,我要做的是创建一个XML解析程序,该程序将从equipment.xml文件中获取所有链接,打开每个文件,读取并查找我要查找的名称标签,然后保存它们字符串列表中的值(Main.XML)。目的是,在读取并将所有必需的值存储在列表中之后,我想逐个索引遍历每个文件的索引,并将innertext的列表innertext中的值替换到另一个文件中。例如,使用新值+后缀。

我已经完成了将近80%的工作,但我一直在苦苦挣扎的是后缀添加了不止一次。那是因为我想不出一个条件,该条件会将写方法限制为仅在读取过程完成后才能执行,但同时也在每个文件上执行。

这是我的代码:

<structure_name> && <from_structure_name>

这是我正在使用的XML文件的一部分:

private void button2_Click(object sender, EventArgs e)
{
    if (textBox1.Text == null)
        MessageBox.Show("Please choose a folder");
    else
    {
        string postfix = textBox2.Text;
        string location = textBox3.Text;
        string workOrder = textBox4.Text;

        List<string> contentList = new List<string>();

        if ((postfix.Length > 0) && (postfix.Length <= 2))
        {
            List<string> nameList = new List<string>();
            List<string> unitNameList = new List<string>();
            List<string> locationList = new List<string>();
            List<string> workOrderList = new List<string>();

            List<string> fileContents = new List<string>();
            // Load the document.
            String folderPath = textBox1.Text;
            XmlDocument doc = new XmlDocument();
            doc.Load(folderPath + "\\Main.xml");
            int i = 0;
            XmlNode root = doc.FirstChild.NextSibling;
            XmlNodeList tree = root.ChildNodes;
            List<string> mainList = new List<string>();

            foreach (XmlNode node in tree)
            {
                mainList.Add(tree.Item(i).InnerText);
                string name = mainList[i];
                var fileName = folderPath + "\\" + name;

                XmlDocument doc1 = new XmlDocument();
                doc1.Load(fileName);

                GetName(fileName, nameList);
                if (nameList.Count > 0 )
                    ReplaceText(fileName, nameList, postfix);

                GetUnitName(fileName, unitNameList);
                if (unitNameList.Count > 0)
                    ReplaceText(fileName, unitNameList, postfix);

                GetLocation(fileName, locationList);
                if (locationList.Count > 0)
                    ReplaceText(fileName, locationList, location);

                GetWorkOrder(fileName, workOrderList);
                if (workOrderList.Count>0)
                   ReplaceText(fileName, workOrderList, workOrder);

                i++;
            }
        }
        else
        {
            MessageBox.Show("Please enter atleast 1 character or 2 at maximum for the postfix.");
        }
    }
}

public void GetName(string url, List<string> list)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(url);
    string search = Path.GetFileNameWithoutExtension(url);
    XmlNodeList nodeList = doc.GetElementsByTagName(search + "_NAME");
    List<string> innerList = new List<string>();
    try
    {
        if (nodeList.Count > 0)
        {
            int i = 0;
            while (i < nodeList.Count)
            {
                if (nodeList.Item(i) != null)
                {
                    var xmlNode = nodeList.Item(i);
                    if (xmlNode != null) innerList.Add(xmlNode.InnerText);
                        string oldName = innerList[i];
                    list.Add(oldName);
                    i++;
                }
            }

        }
    }
    catch
    {
    }
}

public void GetUnitName(string url, List<string> list)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(url);
    string search = Path.GetFileNameWithoutExtension(url);
    XmlNodeList nodeList = doc.GetElementsByTagName(search + "_UNIT_NAME");
    List<string> innerList = new List<string>();

    if (nodeList.Count > 0)
    {
        int i = 0;
        while (i < nodeList.Count)
        {
            if (nodeList.Item(i) != null)
            {
                var xmlNode = nodeList.Item(i);
                if (xmlNode != null) innerList.Add(xmlNode.InnerText);
                    string oldName = innerList[i];
                list.Add(oldName);
                i++;
            }
        }

    }
}

public void GetLocation(string url, List<string> list)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(url);
    string search = Path.GetFileNameWithoutExtension(url);
    XmlNodeList nodeList = doc.GetElementsByTagName("LOCATION_CODE");
    List<string> innerList = new List<string>();
    try
    {
        if (nodeList.Count > 0)
        {
            int i = 0;
            while (i < nodeList.Count && i != nodeList.Count)
            {
                if (nodeList.Item(i) != null)
                {
                    var xmlNode = nodeList.Item(i);
                    if (xmlNode != null) innerList.Add(xmlNode.InnerText);
                        string oldName = innerList[i];

                    list.Add(oldName);
                    i++;
                }
            }

        }
    }
    catch
    {
    }   
}


public void GetWorkOrder(string url, List<string> list)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(url);
    string search = Path.GetFileNameWithoutExtension(url);
    XmlNodeList nodeList = doc.GetElementsByTagName("WO_NAME");
    List<string> innerList = new List<string>();

    if (nodeList.Count > 0)
    {
        int i = 0;
        while (i < nodeList.Count)
        {
            if (nodeList.Item(i) != null)
            {
                var xmlNode = nodeList.Item(i);
                if (xmlNode != null) innerList.Add(xmlNode.InnerText);
                    string oldName = innerList[i];
                list.Add(oldName);
                i++;
            }
        }

    } 
}

public void ReplaceText(string url, List<string> list, string text)
{
    string content = File.ReadAllText(url);
    string old;
    string newText;
    int i = 0;
    while (i < list.Count)
    {
        old = list[i];
        newText = old + text;

        content = content.Replace(old, newText);
        File.WriteAllText(url, content);
        i++;
    }
}

0 个答案:

没有答案
相关问题