如何将结构 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> ";
        }
    }
}
这不是

一件小事。如果你想让一切正常,你必须首先创建一个从 xaml 字符串到对象的分析器。然后从对象解析为 json 字符串。您必须研究并找到库来转换 xml 和 json。

嗨,

我很喜欢这个问题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
        }

最新更新