2011-03-24 43 views
2

我想只顯示來自C#中的XML文件的非可選數據,但我從來沒有使用過它,我是序列化方法中的新成員。如何僅顯示不是可選的xml屬性?在C#中

我有與XSD:

<xs:attribute name="SpecialtyCd" type="xs:string" /> 
    <xs:attribute name="DoctorUid" type="xs:string" /> 
    <xs:attribute name="ValidFrom" type="xs:date" use="optional"/> 
    <xs:attribute name="ValidUntil" type="xs:date" use="optional"/> 

而且我有一個FillEditWindow()語句,我做動態標籤和文本框:

private void FillEditWindow(PropertyInfo p, object dc, Type t) 
{ 
    object[] attributes = p.GetCustomAttributes(true);   

    bool ignore = attributes.Any(a => a.ToString().Contains("XmlIgnoreAttribute")); 
    if (!ignore) 
    { 
     Label lbl = new Label(); 

     whatCategorieName = p.Name; 

     var whatCategorieSource = p.GetValue(dc, null); 
     lbl.Content = whatCategorieName + ':'; 
     lbl.FontFamily = new FontFamily("Verdana"); 
     lbl.FontWeight = FontWeights.Bold; 
     lbl.Width = 400; 

     EditControls.Children.Add(lbl); 

     //Check if Date// 
     if (p.PropertyType == typeof(DateTime)) 
     { 
      DatePicker datePicker = new DatePicker(); 
      datePicker.Name = whatCategorieName; 
      datePicker.Width = 400; 
      DateTime dateSource = DateTime.Parse(whatCategorieSource.ToString()); 

      if (dateSource.ToString() != "1/01/0001 0:00:00") 
      { 
       datePicker.SelectedDate = dateSource; 
      } 

      datePicker.DisplayDateStart = new DateTime(1980, 1, 1); 
      datePicker.DisplayDateEnd = new DateTime(2050, 12, 31); 
      datePicker.FirstDayOfWeek = DayOfWeek.Monday; 

      EditControls.Children.Add(datePicker); 
     } 
     //Check if Boolean 
     else if (p.PropertyType == typeof(Boolean)) 
     { 
      Boolean trueOrFalse = Convert.ToBoolean(whatCategorieSource); 

      CheckBox boxTrueOrFalse = new CheckBox(); 
      boxTrueOrFalse.Name = whatCategorieName; 
      boxTrueOrFalse.Width = 400; 

      EditControls.Children.Add(boxTrueOrFalse); 

      //Check if true or false 
      if (trueOrFalse == true) 
      { 
       boxTrueOrFalse.IsChecked = true; 
      } 
      else if (trueOrFalse == false) 
      { 
       boxTrueOrFalse.IsChecked = false; 
      } 
     } 
     //Check if String 
     else if (p.PropertyType == typeof(string)) 
     { 
      TextBox txt = new TextBox(); 
      txt.Width = 400; 

      if (whatCategorieSource != null) 
      { 
       txt.Name = whatCategorieName; 
       txt.Text = whatCategorieSource.ToString(); 
      } 
      else 
      { 
       txt.Name = whatCategorieName; 
       txt.Text = ""; 
      } 

      EditControls.Children.Add(txt); 
     } 

是否有可能檢查使用if聲明該屬性是否可選?

而且如果是任何幫助這裏就是我從獲得我的數據(當我在一個TreeView點擊某一個項目,我得到一個editscreen與它的所有屬性):

private void TextBlock_PreviewMouseDown(object sender, MouseButtonEventArgs e) 
{ 
    var dc = ((FrameworkElement)e.OriginalSource).DataContext; 

    selectedItemHosp = null; 
    selectedItemList = null; 
    selectedItemExamDef = null; 

    if (dc != null && dc is HospitalWrapper) 
    { 
     if (dc is HospitalWrapper && !((HospitalWrapper)dc).IsTitle) 
     { 
      var context = ((HospitalWrapper)dc).Context; 
      selectedItemHosp = (HospitalWrapper)dc; 

      canSave = true; 
      string edit = ((TextBlock)sender).Text.ToString(); 
      labelEdit.Text = ((TextBlock)sender).Text.ToString(); 

      Type t = context.GetType(); 
      PropertyInfo[] pi = t.GetProperties();     

      EditControls.Children.Clear(); 
      EditControlsLayout.Visibility = Visibility.Visible; 

      sourceOfEdit = "Hospitals"; 

      //Change Data 
      foreach (PropertyInfo p in pi) 
      { 
       if (p.PropertyType == typeof(string) || p.PropertyType == typeof(DateTime) || p.PropertyType == typeof(Boolean)) 
       { 
        FillEditWindow(p, context, t); 
       } 
      } 
     } 

回答

2

對於可選字段需要使用可空類型(見下文)並轉換爲字符串,因爲DateTime?不能表示爲屬性。對於必填項,請將它們初始化爲空字符串:

public class MyClass 
{ 

    public MyClass() 
    { 
     SpecialtyCd = string.Empty; 
     DoctorUid = string.Empty; 
    } 

    [XmlAttribute] 
    public string SpecialtyCd { get; set; } 

    [XmlAttribute] 
    public string DoctorUid { get; set; } 

    [XmlIgnore] 
    public DateTime? ValidFrom { get; set; } 

    [XmlIgnore] 
    public DateTime? ValidUntil { get; set; } 

    [XmlAttribute("ValidUntil")] 
    public string ValidUntilString 
    { 
     get { return ValidUntil.HasValue ? ValidUntil.Value.ToString() : null; } 
     set 
     { 
      ValidUntil = value== null ? (DateTime?) null : DateTime.Parse(value) ; 
     } 
    } 

    [XmlAttribute("ValidFrom")] 
    public string ValidFromString 
    { 
     get { return ValidFrom.HasValue ? ValidFrom.Value.ToString() : null; } 
     set 
     { 
      ValidFrom = value== null ? (DateTime?) null : DateTime.Parse(value) ; 
     } 
    } 
} 
相關問題