如何将struct xaml grid wpf转换为json数组

时间:2017-10-22 05:11:17

标签: c# json wpf html5 xaml

我使用的是xaml wpf网格。我想将struct的网格转换为json。你有什么想法吗?

请看这个例子:

 48 (0 in ASCII value) + 3 = 51.

我想要这个结果:

<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"
    } ],   
};

2 个答案:

答案 0 :(得分:0)

这不是一件轻而易举的事。如果你想要做好,你必须首先从xaml string到object创建一个解析器。然后解析器从对象到json字符串。你必须研究和找到libs来转换xml和json。

答案 1 :(得分:0)

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