이라는 클래스를 작성 중이며이 클래스에는 외부 리소스에서 XML 데이터를 가져 오는 두 가지 정적 메서드가 있습니다. 아래의 예에서는 매우 유사하기 때문에 하나만 보여 드리겠습니다.이 XDocument 코드는 안전한가요?
내가 알아 내려고하는 것은이 코드가 유효하지 않은 URL, 유효하지 않은 데이터 등의 측면에서 "안전"한 것인지 여부입니다. 기본적으로 더 강력합니다. 여기 TEH 코드
private static string XmlUri
{
get { return "path-to-xml-file"; }
}
private static XDocument XmlFile { get; set; }
public int ID { get; set; }
public string Name { get; set; }
public int Parent { get; set; }
/// <summary>
/// Gets a specific category
/// </summary>
/// <param name="id"></param>
/// <returns>A Category with the specified ID</returns>
public static Category Get(int id)
{
try
{
if (XmlFile == null)
XmlFile = XDocument.Load(XmlUri);
}
// Invalid URL or data
catch (Exception ex)
{
// TODO: Log exception
Console.WriteLine(ex.Message);
}
if (XmlFile == null)
return null;
var cat = from category in XmlFile.Descendants("Category")
where category.Attribute("id").Value.ParseSafe() == id
select new Category
{
ID = category.Attribute("id").Value.ParseSafe(),
Parent = category.Attribute("parent").Value.ParseSafe(),
Name = category.Value
};
return cat.SingleOrDefault();
}
외부 XML을 얼마나 제어합니까? 스키마에 대해 유효성을 검사하려고 할 수 있습니다. 또한 여러 스레드가 XML을 읽으려고 할 때 코드가 폭발 할 수도 있습니다 (기술 용어를 사용하려면 ...). – ChaosPandion
@Chaos - XML 파일을 제어 할 수 없으므로 내가 갖고있는 것과 작업해야합니다. thread-safety 부분에 대해 좀 더 설명해 주시겠습니까? 그 시나리오를 실행 해보고 싶지만 확신하고 싶지는 않습니다. 감사! – Marko