Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用.net将XML文件转换为Web表单_C#_Xml - Fatal编程技术网

C# 使用.net将XML文件转换为Web表单

C# 使用.net将XML文件转换为Web表单,c#,xml,C#,Xml,现在我被分配到web项目,该项目使用asp.NETMVC和c。 我需要像下面这样加载一些XML文件,这将告诉我需要创建哪些控件。 这也说明了控件的类型、值、大小和位置。我找不到我该怎么做?请给我正确的方向 <Object type="System.Windows.Forms.Form" name="frmShow" > <Object type="System.Windows.Forms.RadioButton" name="optOne"> <

现在我被分配到web项目,该项目使用asp.NETMVC和c。 我需要像下面这样加载一些XML文件,这将告诉我需要创建哪些控件。 这也说明了控件的类型、值、大小和位置。我找不到我该怎么做?请给我正确的方向

 <Object type="System.Windows.Forms.Form" name="frmShow" >
  <Object type="System.Windows.Forms.RadioButton" name="optOne">    
    <Property name="Size">86, 24</Property>    
    <Property name="Text">Option1</Property>
    <Property name="Location">175, 126</Property>    
  </Object>

  <Object type="System.Windows.Forms.CheckBox" name="chkOne">
    <Property name="Size">84, 24</Property>
    <Property name="Text">CheckOne</Property>
    <Property name="Location">84, 126</Property>  
  </Object>

  <Object type="System.Windows.Forms.TextBox" name="txtOne">
    <Property name="Size">177, 20</Property>
    <Property name="Text">ABC</Property>
    <Property name="Location">84, 88</Property>  
  </Object>

  <Object type="System.Windows.Forms.Label" name="lblOne">
    <Property name="Size">100, 23</Property>
    <Property name="Text">Name</Property>
    <Property name="Location">8, 91</Property>  
  </Object>

  <Object type="System.Windows.Forms.TextBox" name="txtTwo">
    <Property name="Size">177, 20</Property>
    <Property name="Text">Home Address</Property>
    <Property name="Location">84, 50</Property>    
  </Object>

  <Object type="System.Windows.Forms.Label" name="lblTwo">
    <Property name="Size">100, 23</Property>
    <Property name="Text">Address</Property>
    <Property name="Location">7, 53</Property>  
  </Object>

  <ItemDataSet>
    <ItemTable>      
      <TableName>tblItemOne</TableName>
      <Row1>
        <Repeat>True</Repeat>
        <ItemName>Item001</ItemName>
        <Qty>10</Qty>
        <Price>1000</Price>
      </Row1>
      <Row2>
        <Repeat>True</Repeat>
        <ItemName>Item002</ItemName>
        <Qty>20</Qty>
        <Price>2000</Price>
      </Row2>
      <Row3>
        <Repeat>false</Repeat>
      <ItemName>Item003</ItemName>
      <Qty>30</Qty>
      <Price>3000</Price>
      </Row3>      
    </ItemTable>    
  </ItemDataSet>

</Object>

86, 24    
选择1
175, 126    
84, 24
支票
84, 126  
177, 20
基础知识
84, 88  
100, 23
名称
8, 91  
177, 20
家庭住址
84, 50    
100, 23
地址
7, 53  
特布利特蒙
真的
项目001
10
1000
真的
项目002
20
2000
假的
项目003
30
3000

此代码创建控件字典。如果需要,您可以进一步优化它。 另一种选择是基于xml创建模式(xsd),然后按照@Jon Abaca的建议使用XmlSerializer。XmlSerializer选项比这种方法简单得多

第一种方法(没有XmlSerializer)


尝试将此scala代码转换为C#,甚至可以使用C#中的scala代码。请参阅

你也可以使用


它可能是序列化为xaml的Web表单。尝试反序列化它。你能详细解释一下吗?实际上,我不熟悉webform。请给我一些示例或链接,谢谢。XmlSerializer可以将对象转换为XML。XML可以是已序列化的对象。如果是,您可以反序列化XML,并将其转换为对象。情况可能并非如此,但值得一看。
private Dictionary GetControlDictionary(string xmlFilePath)
{
    Dictionary controlsDictionary = new Dictionary();
    Assembly assembly = Assembly.GetAssembly(typeof(System.Windows.Forms.Form));    
    using (XmlReader xmlReader = XmlReader.Create(xmlFilePath))
    {
        XmlDocument xmlDocument = new XmlDocument();
        xmlDocument.Load(xmlReader);
        XmlNodeList xmlNodesList = xmlDocument.SelectNodes("//Object");                        
        foreach (XmlNode xmlNode in xmlNodesList)
        {
            if (xmlNode.Attributes.Count > 0)
            {
                string typeName = xmlNode.Attributes["type"].Value;
                string objectName = xmlNode.Attributes["name"].Value;
                Type controlType = assembly.GetType(typeName);
                if (controlType != null)
                {
                    object controlObject = Activator.CreateInstance(controlType);
                    if (controlObject is Control)
                    {
                        Control control = controlObject as Control;
                        control.Name = objectName;
                        controlsDictionary.Add(objectName, control);
                        foreach (XmlNode childNode in xmlNode.ChildNodes)
                        {
                            if (string.Equals("Property", childNode.Name))
                            {
                                string propertyName = childNode.Attributes["name"].Value;
                                string propertyValue = childNode.InnerText;
                                PropertyInfo propertyInfo = controlType.GetProperty(propertyName);
                                if (propertyInfo != null)
                                {
                                    if(propertyInfo.PropertyType == typeof(System.Drawing.Size))
                                    {
                                        string width = propertyValue.Split(new char[] {','})[0];
                                        string height = propertyValue.Split(new char[] {','})[1];
                                        System.Drawing.Size size = new Size(Convert.ToInt32(width), Convert.ToInt32(height));
                                        propertyInfo.SetValue(control, size, null);
                                    }
                                    else if(propertyInfo.PropertyType == typeof(System.Drawing.Point))
                                    {
                                        string x = propertyValue.Split(new char[] { ',' })[0];
                                        string y = propertyValue.Split(new char[] { ',' })[0];                                        System.Drawing.Point point = new Point(Convert.ToInt32(x), Convert.ToInt32(y));
                                        propertyInfo.SetValue(control, point, null);
                                    }
                                    else if (propertyInfo.PropertyType == typeof(string))
                                    {
                                        propertyInfo.SetValue(control, propertyValue, null);
                                    }
                                }
                            }
                        }
                    }                            
                }
            }
        }
    }

    return controlsDictionary;
}