2017-10-22 157 views
0

我正在使用xaml wpf網格。我想將結構網格轉換爲json。你有什麼想法如何?如何將結構xaml網格wpf轉換爲json數組

請看下面的例子:

<GridBinding> 

    <Grid ID="grd1" ES="9" KFN="" PFN="" GN="گرید 1"> 
    <Column ID="ID" SystemId="517" TableId="3082" FieldId="1" Properties="Header=ID,Visible=True,VisibleIndex=4,GroupIndex=-1,ReadOnly=True,SortIndex=1,SortOrder=Descending,Mask=0,Width=ناعدد," IsCondition="False" IsForce="False" VS="True" ISFormulla="False" VF="1" ReadOnly="True" /> 
    <Column ID="Name" SystemId="517" TableId="3082" FieldId="2" Properties="Header=Name,Visible=True,VisibleIndex=1,GroupIndex=-1,ReadOnly=False,SortIndex=2,SortOrder=Ascending,Mask=0,Width=ناعدد," IsCondition="False" IsForce="False" VS="True" ISFormulla="False" VF="2" ReadOnly="False" /> 
    <Column ID="Family" SystemId="517" TableId="3082" FieldId="3" Properties="Header=Family,Visible=True,VisibleIndex=2,GroupIndex=-1,ReadOnly=False,SortIndex=-1,SortOrder=None,Mask=1,Width=ناعدد," IsCondition="False" IsForce="False" VS="True" ISFormulla="False" VF="3" ReadOnly="False" /> 
    <Column ID="Avg" SystemId="517" TableId="3082" FieldId="10" Properties="Header=Avg,Visible=True,VisibleIndex=3,GroupIndex=-1,ReadOnly=False,SortIndex=-1,SortOrder=None,Mask=0,Width=ناعدد," IsCondition="False" IsForce="False" VS="True" ISFormulla="False" VF="4" ReadOnly="False" /> 
    <Column ID="ردیف" SystemId="0" TableId="0" FieldId="-1" Properties="Header=ردیف,Visible=True,VisibleIndex=0,GroupIndex=-1,ReadOnly=False,SortIndex=-1,SortOrder=None,Mask=0,Fixed=Left,Width=ناعدد," IsCondition="False" IsForce="False" VS="False" ISFormulla="False" VF="0" ReadOnly="False" /> 
    </Grid> 

</GridBinding> 

我想這樣的結果:

var object = { 
    "grd1": [ { 
     ID: "ID", 
     Visible: "True", 
     FieldId: "1", 
     IsForce: "false", 
     ReadOnly="true" 
    } ], 
}; 

這是我的代碼:

if ((item as XmlElement).Attributes["Type"].Value == "Grid") { 
    if (Id_elem == (itemgrigIn as XmlElement).Attributes["ID"].Value) { 
     if (Orientation == "Horizontal" || Orientation == "" || Orientation == null) {     
      s += " <div id=" + Id_elem + "myDiv" + 
        " dir='rtl' align='center' class='table-responsive'></div> " + 
        " <script> $(document).ready(function() {var " + 
        Id_elem + "Divresult = $(" + "'" + "<div id=" + 
        Id_elem + "Div" + " ></div>);" + 
        "'" + " $(" + Id_elem + "Div" + ").append(" + 
        Id_elem + "Divresult); var " + 
        Id_elem + " = new grid(" + "'" + Id_elem + "'" + 
        "," + countgrid + ");" + Id_elem + 
        ".init(); }); </script> "; 
     } 
    } 
} 

回答

0

喜IAM sloved此問題 1-第一次使用的XmlNode

public string CreateHtml(Form formInfo, XmlNode _MainNode,string oldHtml) 
     { 
      ConvertXmlToHtml(_MainNode, ref Result); 
     } 
    public void ConvertXmlToHtml(XmlNode XmlElement, ref string s) 
     { 

      PropertyInfo _propList = new PropertyInfo(); 
    #region grid for create arrye 

      foreach (XmlNode item in XmlElement.ChildNodes) 
      { 
       if (item.Name == "AvailableItems" || item.Name == "DataSource" || item.Name == "GridBinding") 
       { 
        continue; 
       } 
       if ((item as XmlElement).Attributes["Type"].Value == "Grid") 
       { 
        string Id_elem = (item as XmlElement).Attributes["ID"].Value; 

        foreach (XmlNode itemgrigOut in XmlElement.ChildNodes) 
        { 

         if (itemgrigOut.Name == "GridBinding") 
         { 
          countgrid++; 
          if (countgrid < 2) 
            s += "<script> $(document).ready(function() { var object_grid = {" ; 

          foreach (XmlNode itemgrigIn in itemgrigOut) 
          { 
           if (Id_elem == (itemgrigIn as XmlElement).Attributes["ID"].Value) 
           { 
            s += Id_elem + " :[ "; 
            if (Orientation == "Horizontal" || Orientation == "" || Orientation == null) 
            { 
             //ساخت گرید مورد نظر          
            } 
            else 
            { 
             foreach (XmlNode itemcildgrid in itemgrigIn) 
             { 

              if ((itemcildgrid as XmlElement).Attributes[Properties.Resources.PropertiesInfo] != null) 
               _propList = new PropertyInfo() { PropertyList = (itemcildgrid as XmlElement).Attributes[Properties.Resources.PropertiesInfo].Value }; 
              s += " { ID : " + ReturnAttribute((itemcildgrid as XmlElement), "IDgrid", false); 
              s += ", Visible : " + ReturnAttribute((itemcildgrid as XmlElement), "Visible", false); 
              s += ", VisibleIndex : " + ReturnAttribute((itemcildgrid as XmlElement), "VisibleIndex", false); 
              s += ", ReadOnly : " + ReturnAttribute((itemcildgrid as XmlElement), "ReadOnly", false); 
              s += ", SortOrder : " + ReturnAttribute((itemcildgrid as XmlElement), "SortOrder", false); 
              s += ", Mask : " + ReturnAttribute((itemcildgrid as XmlElement), "Mask", false); 
              s += ", IsCondition : " + ReturnAttribute((itemcildgrid as XmlElement), "IsCondition", false); 
              s += ", ISFormulla : " + ReturnAttribute((itemcildgrid as XmlElement), "ISFormulla", false); 
              s += ", ReadOnly : " + ReturnAttribute((itemcildgrid as XmlElement), "ReadOnly", false); 
              s += ", IsForce : " + ReturnAttribute((itemcildgrid as XmlElement), "IsForcegrid", false); 

              s += "}, "; 
             } 
             s += "], "; 

            } 
           } 

          } 


         } 
        } 

       } 


      } 
      if (countgrid == CountAll_grid) 
       s += " } }); </script> "; 
      #endregion 


      #region grid 

      foreach (XmlNode item in XmlElement.ChildNodes) 
      { 
       childCount++; 
       if (item.Name == "AvailableItems" || item.Name == "DataSource" || item.Name == "GridBinding") 
       { 
        continue; 
       } 
       if ((item as XmlElement).Attributes["Type"].Value == "Grid") 
       { 
        string Id_elem = (item as XmlElement).Attributes["ID"].Value; 


        foreach (XmlNode itemgrigOut in XmlElement.ChildNodes) 
        { 

         if (itemgrigOut.Name == "GridBinding") 
         { 
          //شمارش تعداد گریدها 
          countgrid++; 
           if (countgrid ==0) 
          { } 

          foreach (XmlNode itemgrigIn in itemgrigOut) 
          { 
           if (Id_elem == (itemgrigIn as XmlElement).Attributes["ID"].Value) 
           { 
            if (Orientation == "Horizontal" || Orientation == "" || Orientation == null) 
            { 
             //ساخت گرید مورد نظر 
             s += string.Format("<br/><div dir = 'rtl' align = 'center' class='table-responsive'><div class='row well'>" + 
              "<table id=" + Id_elem + "cellpadding='0' cellspacing='0'></table> <div id = pager_" + Id_elem + "></div></div></div>" 
             ); 
            } 
            else 
            { 
             s += " <div id=" + Id_elem + "myDiv" + " dir='rtl' align='center' class='table-responsive'></div> " + 
              " <script> $(document).ready(function() {var " + Id_elem + "Divresult = $(" + "'" + "<div id=" + Id_elem + "Div" + " ></div>" + "'" + ") ; " + 
              " $(" + Id_elem + "Div" + ").append(" + Id_elem + "Divresult); var " + Id_elem + " = new grid(" + "'" + Id_elem + "'" + "," + countgrid + ");" + Id_elem + ".init(); }); </script> "; 
$(document).ready(function() {var object_grid = {" + Id_elem +" :[ "; 


            } 
           } 

          } 


         } 

        } 


      } 
       #endregion 
     } 
0

這不是簡單的任務。如果你想讓所有權利你必須首先創建一個從xaml字符串到對象的解析器。然後從對象解析器到json字符串。您必須研究並查找庫來轉換xml和json。