【问题标题】:How to convert a struct xaml grid wpf to json array如何将 struct xaml 网格 wpf 转换为 json 数组
【发布时间】:2017-10-22 05:11:16
【问题描述】:

我正在使用 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> ";
        }
    }
}

【问题讨论】:

    标签: c# json wpf html xaml


    【解决方案1】:

    这不是一项简单的任务。如果你想没事你必须首先创建一个从 xaml 字符串到对象的解析器。然后从对象解析到 json 字符串。您必须研究并找到转换 xml 和 json 的库。

    【讨论】:

      【解决方案2】:

      嗨,我解决了这个问题 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
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-03-11
        • 2010-09-30
        • 1970-01-01
        • 1970-01-01
        • 2019-05-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多