GVKun编程网logo

如何动态添加XmlInclude属性(xml动态赋值)

23

这篇文章主要围绕如何动态添加XmlInclude属性和xml动态赋值展开,旨在为您提供一份详细的参考资料。我们将全面介绍如何动态添加XmlInclude属性的优缺点,解答xml动态赋值的相关问题,同时

这篇文章主要围绕如何动态添加XmlInclude属性xml动态赋值展开,旨在为您提供一份详细的参考资料。我们将全面介绍如何动态添加XmlInclude属性的优缺点,解答xml动态赋值的相关问题,同时也会为您带来<%@page include%>、<%@include%>、 三者之间的本质区别、android – 如何动态添加onclick到arraylist、angularjs – 如何动态构建ng-include src?、angularjs – 如何动态添加ng-click处理程序的实用方法。

本文目录一览:

如何动态添加XmlInclude属性(xml动态赋值)

如何动态添加XmlInclude属性(xml动态赋值)

我有以下课程

[XmlRoot]public class AList{   public List<B> ListOfBs {get; set;}}public class B{   public string BaseProperty {get; set;}}public class C : B{    public string SomeProperty {get; set;}}public class Main{    public static void Main(string[] args)    {        var aList = new AList();        aList.ListOfBs = new List<B>();        var c = new C { BaseProperty = "Base", SomeProperty = "Some" };        aList.ListOfBs.Add(c);        var type = typeof (AList);        var serializer = new XmlSerializer(type);        TextWriter w = new StringWriter();        serializer.Serialize(w, aList);    }    }

现在,当我尝试运行代码时,我在最后一行收到InvalidOperationException,说

不应使用XmlTest.C类型。使用XmlInclude或SoapInclude属性可以指定静态未知的类型。

我知道在[XmlRoot]中添加[XmlInclude(typeof(C))]属性将解决此问题。但是我想动态地实现它。因为在我的项目中,加载之前不知道C类。C类正在作为插件加载,因此我无法在其中添加XmlInclude属性。

我也尝试过

TypeDescriptor.AddAttributes(typeof(AList), new[] { new XmlIncludeAttribute(c.GetType()) });

之前

var type = typeof (AList);

但没有用。它仍然给出相同的例外。

是否有人对实现它有任何想法?

答案1

小编典典

两种选择;最简单的(但给出奇数的xml)是:

XmlSerializer ser = new XmlSerializer(typeof(AList),    new Type[] {typeof(B), typeof(C)});

带有示例输出:

<AList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <ListOfBs>    <B />    <B xsi:type="C" />  </ListOfBs></AList>

更为优雅的是:

XmlAttributeOverrides aor = new XmlAttributeOverrides();XmlAttributes listAttribs = new XmlAttributes();listAttribs.XmlElements.Add(new XmlElementAttribute("b", typeof(B)));listAttribs.XmlElements.Add(new XmlElementAttribute("c", typeof(C)));aor.Add(typeof(AList), "ListOfBs", listAttribs);XmlSerializer ser = new XmlSerializer(typeof(AList), aor);

带有示例输出:

<AList xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <b />  <c /></AList>

无论哪种情况,您都 必须 缓存并重新使用该ser实例。否则您将因动态编译而流失内存。

<%@page include%>、<%@include%>、<jsp:include > 三者之间的本质区别

<%@page include%>、<%@include%>、 三者之间的本质区别

<%@page include%>、<%@include%>、<jsp:include>三者之间的本质区别

先从它的几个内置对象说起。

application和session比较简单,这里主要说明request和page的作用范围。

application:全局作用范围,整个应用程序共享,就是在部署文件中的同一个webApp共享,生命周期为:应用程序启动到停止。

session:会话作用域,当用户首次访问时,产生一个新的会话,以后服务器就可以记住这个会话状态。生命周期:会话超时,或者服务器端强制使会话失效。

request:请求作用域,就是客户端的一次请求。

page:一个JSP页面。

以上作用范围使越来越小, request和page的生命周期都是短暂的,他们之间的区别就是:一个request可以包含多个page页(include,forward以及filter)。举个简单的例子:

jsp1.jsp

<jsp:useBean id="beanID" class="xxx .xxx.BeanClass" scope="request" />

<jsp:include page="jsp2.jsp" />

jsp2.jsp

<jsp:getProperty name="beanID" property="sample" />

运行jsp1.jsp可以正常显示数据,因为作用范围是request,相当于调用了request.setAttribute()方法,

jsp2页通过request.getAttribute来获得这个bean.如果把request改为page,就会抛出NullPointerExceptioin.

一个请求可以跨好几个JSP(include和forword)和Servlet(如filter).不过这里有一个需要注意的就是区别

<jsp:include> 和<%@ include %>.如果上面的<jsp:include page="jsp2.jsp" />改为<%@ include file="jsp2.jsp" %>,即使scope为page也是正确的,<%@ include %>是编译时包含,<jsp:include>是运行时包含.前者就相当于宏,编译时替代,后者相当于函数,运行时返回.

<% @ page %> 只能包含静态内容。

android – 如何动态添加onclick到arraylist

android – 如何动态添加onclick到arraylist

我从DB获取标题列表,并使用ArrayList将这些标题添加到布局中.此布局是使用适配器创建的.当我点击此布局中显示的标题时,我想打开另一个布局.如何解决这个问题?有什么建议请….

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.httpentity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class FullDetails extends ListActivity  {

    ArrayList<String> listItems=new ArrayList<String>();
ArrayAdapter<String> adapter;
    private ListView lv;
    int clickCounter=0;
    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.activity_full_details);
       adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,listItems);
         setlistadapter(adapter);
        fullItems(null);


    }
JSONArray all=new JSONArray();
    String size;
    public void fullItems(View v) {
        String result = "";
         InputStream is=null;
        try{

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://MOTal.com/and/events.jsp");
                HttpResponse response = httpclient.execute(httppost);
                        httpentity entity = response.getEntity();
                     is = entity.getContent();
        }catch(Exception e){
                Log.e("log_tag", "Error in http connection "+e.toString());
        }
        try{
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);

                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                        sb.append(line + "\n");
                }
                is.close();

                result=sb.toString();
        }catch(Exception e){
                Log.e("log_tag", "Error converting result "+e.toString());
        }
        try{
                JSONArray jArray = new JSONArray(result);
                            for(int i=0;i<jArray.length();i++){

                        JSONObject json_data = jArray.getJSONObject(i);

                    size=json_data.getString("size");

                   for(int j=0;j<Integer.parseInt(size);j++){

                     all=json_data.getJSONArray("res"+j);
listItems.add(all.get(0)+" \n venue:"+all.get(1)+" Price:"+all.get(3));
                   }

                }

        }
        catch(JSONException e){
                Log.e("log_tag", "Error parsing data "+e.toString());
        }

        adapter.notifyDataSetChanged();
    }
}

activity_full_details.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:drawSelectorOnTop="false"
    />
</LinearLayout>

解决方法:

您正在寻找的是列表项单击侦听器.你可以尝试下面的lv.setonItemCLickListener就是你的例子.

listView.setonItemClickListener(new OnItemClickListener() {
  @Override
  public void onItemClick(AdapterView<?> parent, View view,
    int position, long id) {
    Toast.makeText(getApplicationContext(),
      "Click ListItem Number " + position, Toast.LENGTH_LONG)
      .show();
  }
}); 

按照这个基本example

并且没有像onClick到ArrayList那样,为视图设置了点击.

angularjs – 如何动态构建ng-include src?

angularjs – 如何动态构建ng-include src?

我有以下代码:
<div ng-repeat="module in modules" id="{{module.Id}}">
    <ng-include ng-init="bootstrapModule(module.Id)" src=""></ng-include>
</div>

我想要像src一样在src中构建一个字符串:

/modules/{{module.Name}}/{{module.Name}}.tpl.html

但我继续打路障。我试图使用回调函数来构建它,

$scope.constructTemplateUrl = function(id) {
    return '/modules/' + id + '/' + id + '.tpl.html';
}

但是,结束结束了,似乎并不喜欢。我也试图像这样构建它:

ng-src="/modules/{{module.Id}}/{{module.Id}}.tpl.html"

但是这也不行。而不是花时间在灌木丛周围跳动,我想知道其他任何人是否反对这样的事情,有什么想法?

另外,当我从$资源获取模块时,我以$ q异步返回它们,所以我似乎无法通过控制器将其添加到模块中,因为$ scope.modules只等于一个函数那一点

有任何想法吗?

ngInclude | src指令需要一个角度表达式,这意味着你应该写

ng-src =“’/ modules /’module.Id’/tpl.html’”

从http://docs.angularjs.org/api/ng.directive:ngInclude

ngInclude|src string angular expression evaluating to URL. If the
source is a string constant,make sure you wrap it in quotes,e.g.
src=”‘myPartialTemplate.html'”.

如果您在模型中构建url而不是内联HTML可能会更好

<div ng-repeat="module in modules" id="{{module.Id}}">
    <ng-include src="module.url"></ng-include>
</div>

angularjs – 如何动态添加ng-click处理程序

angularjs – 如何动态添加ng-click处理程序

我尝试在(动态)之前生成的按钮上添加ng-click,但效果不佳.此外,我已尝试在此论坛上找到的所有解决方案,没有人运作良好.

我的HTML代码:

<bodyng-app="myApp">
    <divng-controller="myCtrl">
        <div id="play" tabindex="0" ng-init="init()" ng-keydown="keyDown($event)">
            {{ content }}
        </div>
    </div>

    <script src="js/angular.min.js"></script>
    <script src="js/script.js"></script>
</body>

我的AngularJS代码:

var app = angular.module("myApp",[]);
app.controller("myCtrl",function($scope,$compile) {
  $scope.init = function() {
    var el = '<buttonid="start" data-ng-click="startAnimation()">Start</buttom>';
    var element = angular.element(document.querySelector('#play'));
    var generated = element.html(el);
    $compile(generated)($scope);
}
$scope.startAnimation = function(){
        console.log("click");
}
});

我的错误是“RangeError:超出最大调用堆栈大小”,这是由$compile(生成)($scope)生成的; .从第一个问题派生出来的另一个问题是,如果我单击按钮,那么函数startAnimation将执行数百次.

请给我一个解决方案.哪里出错了.

解决方法

问题出在这行代码中:

$compile(generated)($scope);

相反它应该是:

$compile(generated.contents())($scope);

今天关于如何动态添加XmlInclude属性xml动态赋值的讲解已经结束,谢谢您的阅读,如果想了解更多关于<%@page include%>、<%@include%>、 三者之间的本质区别、android – 如何动态添加onclick到arraylist、angularjs – 如何动态构建ng-include src?、angularjs – 如何动态添加ng-click处理程序的相关知识,请在本站搜索。

本文标签: