如何改善此代码的性能和设计

时间:2019-09-18 09:46:46

标签: c#

下面是我当前正在使用的代码,但是有没有办法使它更简洁,更短?我希望我的代码看起来效率低下,外观漂亮。我的老师说了一些“用循环做”,但我不知道该循环能做什么以及如何工作。

        {
            XmlWriter xmlWriter = XmlWriter.Create(PATH);

            xmlWriter.WriteStartDocument();
            xmlWriter.WriteStartElement("Config");

            xmlWriter.WriteStartElement("TBSOMS");
            xmlWriter.WriteString(TBSOMS.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBWVB");
            xmlWriter.WriteString(TBWVB.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBWNB");
            xmlWriter.WriteString(TBWNB.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBASPMM1");
            xmlWriter.WriteString(TBASPMM1.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBASPMM2");
            xmlWriter.WriteString(TBASPMM2.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBDUM");
            xmlWriter.WriteString(TBDUM.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBADPR");
            xmlWriter.WriteString(TBADPR.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteStartElement("TBAR");
            xmlWriter.WriteString(TBAR.Text);
            xmlWriter.WriteEndElement();

            xmlWriter.WriteEndDocument();

            xmlWriter.Close();
        }



3 个答案:

答案 0 :(得分:1)

我将从这样的东西开始...

var values = new Dictionary<string, string>
{
    { nameof(TBWVB), TBWVB.Text },
    { nameof(TBWNB), TBWNB.Text }
    //        .... etc ......
}

XmlWriter xmlWriter = XmlWriter.Create(PATH);

xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Config");

foreach (var item in values.Keys)
{
    xmlWriter.WriteStartElement(item);
    xmlWriter.WriteString(values[item]);
    xmlWriter.WriteEndElement();
}

答案 1 :(得分:1)

类似的东西:

private void writeElement(XmlWriter xmlwrtr, String element, var obj){
        xmlwrtr.WriteStartElement(element);
        xmlwrtr.WriteString(obj.Text);
        xmlwrtr.WriteEndElement();
}

愿意提供:

 {
        XmlWriter xmlWriter = XmlWriter.Create(PATH);
        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("Config");

        writeElement(xmlWriter, "TBSOMS", TBSOMS)
        writeElement(xmlWriter, "TBWVB", TBWVB)
        // And so on

        xmlWriter.WriteEndDocument();

        xmlWriter.Close()
}

答案 2 :(得分:0)

通过创建类变量,您可以通过多种方法访问其值。

private static XmlWriter xmlWriter;
public void Write()
{
    xmlWriter = XmlWriter.Create(Path);
    xmlWriter.WriteStartDocument();
    xmlWriter.WriteStartElement("Config");

    new List<(elementDataType element, string name)> // note that you need to change "elementDataType" to your elements data type
    {
        (TBSOMS, nameof(TBSOMS)),
        (TBWVB, nameof(TBWVB)),
        // ... just list all elements in here
    }.ForEach(elem => WriteElement(elem.element, elem.name));


    xmlWriter.WriteEndDocument();
    xmlWriter.Close();
}

private void WriteElement(var element, string name) //note that you need to change var to your elements datatype
{
    xmlWriter.WriteStartElement(name);
    xmlWriter.WriteString(element.Text);
    xmlWriter.WriteEndElement();
}