c# - 如何只显示非可选的 xml 属性?在 C# 中

标签 c# xml serialization xml-serialization

我只想在 C# 中显示来自 XML 文件的非可选数据,但我以前从未使用过它,而且我是序列化方法的新手。

我有一个 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 中的某个项目时,我会得到一个包含所有属性的编辑屏幕):

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);
                }
            }
        }

最佳答案

对于可选字段,您需要使用可空类型(见下文)并转换为字符串,因为 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) ;
        }
    }
}

关于c# - 如何只显示非可选的 xml 属性?在 C# 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5418964/

相关文章:

c# - 表单必须可转换为 System.Web.UI.Page

java - 服务器通过网络使用Java序列化是否存在安全漏洞?

serialization - 是什么让 Smalltalk 能够进行图像持久化,为什么像 Ruby/Python 这样的语言不能序列化自己?

ios - Swift 中的序列化、反序列化到 JSON

C# 从给定接口(interface)的字符串实例化类

c# - 如何在 Web Api Controller 中安全地获取本地化字符串?

java - Android XzingScanner : How to customise ZxingScanner layout ?(添加按钮)

xml - AIX 中最简单的嵌套 XML 解析

xml - xsl/xpath 选择最多的 sibling ,但不选择下一个相似的 sibling

c# - 基于运行时参数类型动态选择方法