Member 12692000 Ответов: 1

Как написать имя файла, в котором ошибка возникает только один раз в моей программе?


У меня есть несколько xml-файлов, которые выглядят так
<ref-list>
  <title>REFERENCES</title>
  <ref id="ref1">
    <label>[1]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angel’skii</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A.,G.</given-names></string-name>, <string-name><surname>Arkhelyuk</surname>, <given-names>A.,D.</given-names></string-name>, <string-name><surname>Ermolenko</surname>, <given-names>S.,B.</given-names></string-name>, <string-name><surname>Burkovets</surname>, <given-names>D.,N.</given-names></string-name></person-group>, "<article-title>Scattering of laser radiation by multifractal biological structures</article-title>." <source>Optika i Spektroskopiya 88</source> (<issue>3</issue>), <fpage>495</fpage>–<lpage>498</lpage> (<year>2000</year>).</mixed-citation>
  </ref>
  <ref id="ref2">
    <label>[2]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Maksimyak</surname>, <given-names>P.,P.</given-names></string-name>, <string-name><surname>Hanson</surname>, <given-names>S.,G.</given-names></string-name>, <string-name><surname>Ryukhin</surname>, <given-names>V.,V.</given-names></string-name></person-group>, "<article-title>New Feasibilities for Characterizing Rough Surfaces by Optical-Correlation Techniques</article-title>" <source>Applied Optics</source> (<issue>40</issue>) , pp. <fpage>5693</fpage>–<lpage>5707</lpage> (<year>2001</year>).</mixed-citation>
  </ref>
  <ref id="ref3">
    <label>[3]</label>
    <mixed-citation publication-type="conf-proc">
      <person-group person-group-type="author">
        <string-name>
          <surname>Ushenko</surname>, <given-names>Yu., A.</given-names></string-name>, <string-name><surname>Dubolazov</surname>, <given-names>A.,V.</given-names></string-name>, <string-name><surname>Karachevtcev</surname>, <given-names>A., O.</given-names></string-name>, <string-name><surname>Zabolotna</surname>, <given-names>N., I.</given-names></string-name></person-group>, "<article-title>A fractal and statistic analysis of Mueller-matrix images of phase inhomogeneous layers</article-title>." <conf-name>Proceedings SPIE</conf-name><volume>8134</volume>, <fpage>P81340P4</fpage> (<year>2011</year>).</mixed-citation>
  </ref>
  <ref id="ref4">
    <label>[4]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Hanson</surname>, <given-names>S., G.</given-names></string-name>, <string-name><surname>Zenkova</surname>, <given-names>C.,Yu.</given-names></string-name>, <string-name><surname>Gorsky</surname>, <given-names>M.,P.</given-names></string-name>, <string-name><surname>Gorodyns’ka</surname>, <given-names>N.,V.</given-names></string-name></person-group>, "<article-title>On polarization metrology (estimation) of the degree of coherence of optical waves</article-title>." <source>Optics Express</source><volume>17</volume>(<issue>18</issue>), pp.<fpage>15623</fpage>–<lpage>15634</lpage> (<year>2009</year>).</mixed-citation>
  </ref>
  <ref id="ref5">
    <label>[5]</label>
    <mixed-citation publication-type="conf-proc">
      <person-group person-group-type="author">
        <string-name>
          <surname>Ushenko</surname>
          <given-names>O.,G.</given-names>
        </string-name>, <string-name><surname>Dubolazov</surname>, <given-names>A.</given-names></string-name>, <string-name><surname>Balanets’ka</surname>, <given-names>V.</given-names></string-name>, <string-name><surname>Karachevtsev</surname>, <given-names>A.</given-names></string-name>, <string-name><surname>Sydor</surname>, <given-names>M.</given-names></string-name></person-group>, "<article-title>Wavelet analysis for polarization inhomogeneous laser images of blood plasma</article-title>." <conf-name>Proc. SPIE</conf-name><volume>8338</volume>, P. <fpage>83381H</fpage> (<year>2011</year>).</mixed-citation>
  </ref>
  <ref id="ref6">
    <label>[6]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Ushenko</surname>, <given-names>Yu.,O.</given-names></string-name>, <string-name><surname>Dubolazov</surname>, <given-names>O., V.</given-names></string-name>, <string-name><surname>Karachevtsev</surname>, <given-names>A.,O.</given-names></string-name>, <string-name><surname>Gorsky</surname>, <given-names>M., P.</given-names></string-name>, <string-name><surname>Marchuk</surname>, <given-names>Yu., F.</given-names></string-name></person-group>, "<article-title>Wavelet analysis of Fourier polarized images of the human bile</article-title>." <source>Applied Optics</source> (<issue>51</issue>), P. <fpage>133</fpage>–<lpage>139</lpage> (<year>2012</year>).</mixed-citation>
  </ref>
  <ref id="ref7">
    <label>[7]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A.,G.</given-names></string-name>, <string-name><surname>Burkovets</surname>, <given-names>D.,N.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>Y., A.</given-names></string-name></person-group>, "<article-title>Polarization visualization and selection of biotissue image two-layer scattering medium</article-title>." <source>Journal of biomedical optics</source><volume>10</volume>(<issue>1</issue>), P.<fpage>14010</fpage> (<year>2005</year>).</mixed-citation>
  </ref>
  <ref id="ref8">
    <label>[8]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Polyanskii</surname>, <given-names>P.,V.</given-names></string-name>, <string-name><surname>Felde</surname>, <given-names>C.,V.</given-names></string-name></person-group>, "<article-title>The emerging field of correlation optics</article-title>." <source>Optics and Photonics News</source><volume>23</volume>(<issue>4</issue>), p.p.<fpage>25</fpage>–<lpage>29</lpage> (<year>2012</year>).</mixed-citation>
  </ref>
  <ref id="ref9">
    <label>[9]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>O.,V.</given-names></string-name>, <string-name><surname>Bekshaev</surname>, <given-names>A.,Ya.</given-names></string-name>, <string-name><surname>Maksimyak</surname>, <given-names>P.,P.</given-names></string-name>, <string-name><surname>Maksimyak</surname>, <given-names>A.,P.</given-names></string-name>, Mokhun, <string-name><surname>Hanson</surname>, <given-names>S.,G.</given-names></string-name>, <string-name><surname>Zenkova</surname>, <given-names>C., Yu.</given-names></string-name>, <string-name><surname>Tyurin</surname>, <given-names>A.,V.</given-names></string-name></person-group>, "<article-title>Circular motion of particles suspended in a Gaussian beam with circular polarization validates the spin part of the internal energy flow</article-title>." <source>Optics Express</source><volume>20</volume>(<issue>10</issue>), pp.<fpage>11351</fpage>–<lpage>11356</lpage> (<year>2012</year>).</mixed-citation>
  </ref>
  <ref id="ref10">
    <label>[10]</label>
    <mixed-citation publication-type="conf-proc">
      <person-group person-group-type="author">
        <string-name>
          <surname>Arkhelyuk</surname>, <given-names>A.</given-names></string-name>, <string-name><surname>Podkamen</surname>, <given-names>L.</given-names></string-name>, <string-name><surname>Glibka</surname>, <given-names>V.</given-names></string-name></person-group>, "<article-title>Characteristics investigations of surface and volumetrical scattering of the polarised radiation by a layer of oriented particls</article-title>." <conf-name>Proc. SPIE</conf-name><volume>5477</volume>, <fpage>P171</fpage>–<lpage>176</lpage> (<year>2004</year>).</mixed-citation>
  </ref>
  <ref id="ref11">
    <label>[11]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>O.V.</given-names></string-name>, <string-name><surname>Besaha</surname>, <given-names>R.N.</given-names></string-name>, <string-name><surname>Mokhun</surname>, <given-names>I.I.</given-names></string-name></person-group> "<article-title>Appearance of wavefront dislocations under interference among beams with simple wavefronts</article-title>," <source>Optica Applicata</source><volume>27</volume>(<issue>4</issue>), Pages <fpage>272</fpage>–<lpage>278</lpage> (<year>1997</year>).</mixed-citation>
  </ref>
  <ref id="ref12">
    <label>[12]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>P., O.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A., G.</given-names></string-name>, <string-name><surname>Dubolazov</surname>, <given-names>A., V.</given-names></string-name>, <string-name><surname>Sidor</surname>, <given-names>M., I.</given-names></string-name>, <string-name><surname>Bodnar</surname>, <given-names>G., B.</given-names></string-name>, <string-name><surname>Koval</surname>, <given-names>G.</given-names></string-name>, <string-name><surname>Trifonyuk</surname>, <given-names>L.</given-names></string-name></person-group>, "<article-title>The singular approach for processing polarization-inhomogeneous laser images of blood plasma layers</article-title>." <source source-type="IEFF">J. Opt.</source> (<issue>15</issue>), <fpage>044030</fpage> (<year>2013</year>).</mixed-citation>
  </ref>
  <ref id="ref13">
    <label>[13]</label>
    <mixed-citation publication-type="journal">
      <person-group person-group-type="author">
        <string-name>
          <surname>Angelsky</surname>, <given-names>P., O.</given-names></string-name>, <string-name><surname>Ushenko</surname>, <given-names>A., G.</given-names></string-name>, <string-name><surname>Dubolazov</surname>, <given-names>A., V.</given-names></string-name>, <string-name><surname>Sidor</surname>, <given-names>M., I.</given-names></string-name>, <string-name><surname>Bodnar</surname>, <given-names>G., B.</given-names></string-name>, <string-name><surname>Koval</surname>, <given-names>G.</given-names></string-name>, <string-name><surname>Trifonyuk</surname>, <given-names>L.</given-names></string-name></person-group>, "<chapter-title>The singular approach for processing polarization-inhomogeneous laser images of blood plasma layers</chapter-title>." J. Opt. (<issue>15</issue>), <fpage>044030</fpage> (<year>2013</year>).</mixed-citation>
  </ref>
</ref-list>

Я хочу найти каждый отдельный узел по имени mixed-citation в файле и проверьте, есть ли какие-либо дочерние узлы mixed-citation узел по имени article-title или chapter-title, если найден, то проверьте, есть ли еще один дочерний узел по имени source в том же mixed-citation, если нет source найдется, получим его родительский узел ref и запишите это в какой-нибудь журнал.

Что я уже пробовал:

public static void Main(string[] args)
{
	string path=@"D:\test\xml";
	var files=Directory.GetFiles(path,"*.xml");
	foreach (var file in files)
	{
		string content=File.ReadAllText(file);
		var doc=TryParseXDocument("<root>"+content+"</root>");
		if (doc!=null)
		{
			var refIds=doc.Descendants("mixed-citation");
			try
			{
				foreach (var refId in refIds)
				{
					if (refId.Descendants("article-title").Any()||refId.Descendants("chapter-title").Any())
					{
						if (refId.Descendants("source").Count()<1)
						{
							string filePath = path + @"\Error.log";
							using (StreamWriter writer = new StreamWriter(filePath, true))
							{
								writer.WriteLine(file+"\r\n---------------------------------------------------\r\nCheck "+refId.Parent.Attribute("id")+" for possible missing <source>");
							}
						}
					};
				}
			}
			catch
			{
			}
			
			
		}
		else
		{
			string filePath = path + @"\InvalidXML.log";
			using (StreamWriter writer = new StreamWriter(filePath, true))
			{
				writer.WriteLine(file + ":\r\n---------------------------------------------------\r\nInvalid XML file, check Tidy parsing and invalid entities");
			}
		}
	}
	
	
	Console.ReadLine();
}
public static XDocument TryParseXDocument(string xmlContent)
{
	
	try
	{
		return XDocument.Parse(xmlContent);
	}
	
	catch(Exception)
	{
		return null;
	}
	
}

Теперь проблема в том, что я получаю тексты в файле Error.log в формате
D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref3" for possible missing <source>
D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref5" for possible missing <source>
D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref10" for possible missing <source>
D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref13" for possible missing <source>

В то время как я хочу, чтобы это было похоже
D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref3" for possible missing <source>
Check id="ref5" for possible missing <source>
Check id="ref10" for possible missing <source>
Check id="ref13" for possible missing <source>

Как мне это сделать?
Кроме того, как можно оптимизировать код?

1 Ответов

Рейтинг:
9

Wendelius

Почему бы не собрать ошибки в stringbuilder, а затем записать данные в конце концов.

Что-то вроде

string path = @"D:\test\xml";
System.Text.StringBuilder errors;
bool hasErrors;

var files = System.IO.Directory.GetFiles(path, "*.xml");
foreach (var file in files) {
   hasErrors = false;
   errors = new System.Text.StringBuilder();
   errors.AppendLine($"File: {file}");
   errors.AppendLine("---------------------------------------");
   string content = System.IO.File.ReadAllText(file);
   var doc = TryParseXDocument("<root>" + content + "</root>");
   if (doc != null) {
      var refIds = doc.Descendants("mixed-citation");
      try {
         foreach (var refId in refIds) {
            if (refId.Descendants("article-title").Any() || refId.Descendants("chapter-title").Any()) {
               if (refId.Descendants("source").Count() < 1) {
                  hasErrors = true;
                  errors.AppendLine($"Check {refId.Parent.Attribute("id")}for possible missing <source> ");
               }
            };
         }
      } catch {
      }
   } else {
      string filePath = path + @"\InvalidXML.log";
      using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filePath, true)) {
         writer.WriteLine(file + ":\r\n---------------------------------------------------\r\nInvalid XML file, check Tidy parsing and invalid entities");
      }
   }
   if (hasErrors) {
      using (System.IO.StreamWriter errorFile = new System.IO.StreamWriter(path + @"\Error.log", true)) {
         errorFile.WriteLine(errors.ToString());
      }
   }
}


Console.ReadLine();


Member 12692000

Спасибо...кстати, может ли код внутри последнего цикла foreach т. е. foreach (var refId in refIds) быть оптимизированным или улучшенным?

Wendelius

Это зависит от требований, которые вы предъявляете к данным, и от того, какие данные вы ожидаете получить. Но в целом, если объем данных невелик, я думаю, что попытка оптимизировать эту часть может не стоить усилий.

Другое дело-пустой блок try..catch. Вы не должны ловить исключения, если вы хотя бы не сообщаете о них. С текущим кодом у вас могут быть исключения и проблемы при чтении данных, но вы об этом не знаете.

Member 12692000

Привет, я получаю дубликаты результатов ошибок нескольких файлов, т. е.
D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref3" for possible missing <source>
Check id="ref5" for possible missing <source>
Check id="ref10" for possible missing <source>
Check id="ref13" for possible missing <source>

D:\test\xml\ref.xml
---------------------------------------------------
Check id="ref3" for possible missing <source>
Check id="ref5" for possible missing <source>
Check id="ref10" for possible missing <source>
Check id="ref13" for possible missing <source>

D:\test\xml\ref2.xml
---------------------------------------------------
Check id="ref9" for possible missing <source>
Check id="ref20" for possible missing <source>

D:\test\xml\ref2.xml
---------------------------------------------------
Check id="ref9" for possible missing <source>
Check id="ref20" for possible missing <source>

Wendelius

К сожалению, мой плохой. Забыл инициализировать stringbuilder на каждой итерации. Смотрите обновленный код.

Member 12692000

Спасибо

Wendelius

Пожалуйста