2010-12-15 4 views
0

今、私はasp.net mvcをC#で使用しているWebプロジェクトに割り当てられています。 以下のようないくつかのXMLファイルをロードする必要があります。これは、どのコントロールを作成する必要があるかを示します。 これはまた、コントロールの種類、その値、サイズ、location.iが私のやり方を見つけることができないことを教えてくれますか?私を正しい方法で案内してください。XMLファイルをWebフォームに。

<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> 
+0

webf xamlにシリアライズされたorm。デシリアライズを試みてください。 –

+0

あなたは私に詳細を教えてもらえますか?実際、私はwebform.pleaseに慣れていません。 – Chong

+0

XmlSerializerは、オブジェクトをXMLに変換できます。 XMLは、シリアル化されたオブジェクトである可能性があります。 そうであれば、XMLを逆シリアル化してオブジェクトに変換できます。それは事実ではないかもしれないが、 それは一見価値がある。 http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx –

答えて

2

このコードは、コントロールの辞書を作成します。必要に応じてさらに最適化することができます。 もう1つの方法は、xmlに基づいてスキーマ(xsd)を作成し、@ Jon Abacaの提案に従ってXmlSerializerを使用する方法です。 XmlSerializerオプションは、このアプローチよりもはるかに簡単です。

(XmlSerializerをせずに)最初のアプローチ

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; 
}
関連する問題