Параметры IIFE против декларации

У меня есть предложение в следующий раз:

string nameFile = "Test.xml";
bool newFile = false;


if (!File.Exists(nameFile))
{
    newFile = true;
    XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
    xmlWriterSettings.Indent = true;
    xmlWriterSettings.NewLineOnAttributes = true;

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

    xmlWriter = XmlWriter.Create("Test.xml", xmlWriterSettings))
}
else
{
    doc = new XmlDocument();
    doc.Load(nameFile);

    // Create a XPathNavigator
    // You can go where you want to add
    // In this case it is just after last child of the roor
    XPathNavigator navigator = doc.CreateNavigator();     

    navigator.MoveToChild("School", "");
    xmlWriter = navigator.AppendChild();
}

// From here you can work only with xmlWriter,
// One will point on a file and the other on the stream of xmlDocument
// So you will need to save the document in the second choise

xmlWriter.WriteStartElement("Student");
xmlWriter.WriteElementString("FirstName", firstName);
xmlWriter.WriteElementString("LastName", lastName);
xmlWriter.WriteEndElement();


// End document / close or save. 
if (newFile)
    xmlWriter.WriteEndDocument();

xmlWriter.Close();

if (!newFile)
    doc.Save(nameFile);

Он должен работать. :)

0
задан briangrimaldo 5 March 2019 в 16:40
поделиться

1 ответ

Это техника, используемая минифайерами для сохранения ключевого слова var, если уже есть IIFE.

Нет веской причины писать такой исходный код, это сбивает с толку.

0
ответ дан Bergi 5 March 2019 в 16:40
поделиться
Другие вопросы по тегам:

Похожие вопросы: