- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我有一个很大的 .net 类和一些 xslt 文件。我正在序列化我的对象以使用我的 xslt 文件进行转换。
我的类(class)名称是 Application,它有一个包含应用程序集合的 Applicant 属性。
public class Application
{
public Person Applicant { get; set; }
}
public class Person
{
public List<Application> Applications { get; set; }
}
当我序列化我的类的实例时,通常我获得的 Xml 包含 z:Ref="i18" 属性以防止无限创建 Xml 来描述现有的引用属性。但是这种情况改变了我必须在 Xslt 文件中编写的所需 Xpath 表达式。
我是否有机会序列化包含真实实体值而不是 z:Ref 标签的指定深度的对象?
这是我的序列化代码:
public string Serialize(object input)
{
XmlDocument XmlDoc = new XmlDocument();
DataContractSerializer xmlDataContractSerializer =
new DataContractSerializer(input.GetType());
MemoryStream MemStream = new MemoryStream();
try
{
xmlDataContractSerializer.WriteObject(MemStream, input);
MemStream.Position = 0;
XmlDoc.Load(MemStream);
return XmlDoc.InnerXml;
}
finally
{
MemStream.Close();
}
}
提前致谢
阿尼尔
最佳答案
不,基本上。但是,您应该能够使用类似的东西:
<xsl:key name="ids" match="*[@z:Id]" use="@z:Id"/>
然后使用xsl key
函数传递当前节点的@z:Ref
,其中z
是一个xmlns
http://schemas.microsoft.com/2003/10/Serialization/
的别名 - 这至少会在整个过程中保持相同的用法。
完整示例 - 首先是 xslt(“my.xslt”):
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/"
xmlns:dcs="http://schemas.datacontract.org/2004/07/"
>
<xsl:key name="ids" match="*[@z:Id]" use="@z:Id"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="*[@z:Ref]">
<xsl:param name="depth" select="5"/>
<xsl:apply-templates select="key('ids', @z:Ref)">
<xsl:with-param name="depth" select="$depth"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="*[@z:Id]">
<xsl:param name="depth" select="5"/>
<xsl:value-of select="$depth"/>: <xsl:value-of select="name()"/><xsl:text xml:space="preserve">
</xsl:text>
<xsl:if test="$depth > 0">
<xsl:apply-templates select="dcs:*">
<xsl:with-param name="depth" select="($depth)-1"/>
</xsl:apply-templates>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
请注意,这通过 $depth
参数(递减)走了 5 个级别;关键部分是任何元素 *[@z:Ref]
上的初始 match
,然后使用 key
将相同的请求代理到原始元素,通过 @z:Id
解析。这意味着当我们移动到子元素时,我们只需要使用类似的东西:
<xsl:apply-templates select="dcs:*"/>
虽然我们显然可以更细化,例如:
<xsl:apply-templates select="dcs:Foo"/>
另请注意,要添加特定于 Foo
的 match
,您需要添加:
<xsl:template match="dcs:Foo[@z:Id]"><!-- --></xsl:template>
确保我们的 *[@z:Ref]
匹配继续处理引用转发。
还有 C#:
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
[DataContract]
public class Foo
{
[DataMember]
public Bar Bar { get; set; }
}
[DataContract]
public class Bar
{
[DataMember]
public Foo Foo { get; set; }
}
static class Program
{
static void Main()
{
var foo = new Foo();
var bar = new Bar();
foo.Bar = bar;
bar.Foo = foo;
using (var ms = new MemoryStream())
{
var ser = new DataContractSerializer(typeof(Foo), new DataContractSerializerSettings {
PreserveObjectReferences = true
});
ser.WriteObject(ms, foo);
Console.WriteLine(Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length));
Console.WriteLine();
ms.Position = 0;
var xslt = new XslCompiledTransform();
xslt.Load("my.xslt");
using (var reader = XmlReader.Create(ms))
{
xslt.Transform(reader, null, Console.Out);
}
}
Console.WriteLine();
Console.WriteLine("press any key");
Console.ReadKey();
}
}
关于c# - 使用 XSLT 转换序列化的 .net 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13339999/
我的一位教授给了我们一些考试练习题,其中一个问题类似于下面(伪代码): a.setColor(blue); b.setColor(red); a = b; b.setColor(purple); b
我似乎经常使用这个测试 if( object && object !== "null" && object !== "undefined" ){ doSomething(); } 在对象上,我
C# Object/object 是值类型还是引用类型? 我检查过它们可以保留引用,但是这个引用不能用于更改对象。 using System; class MyClass { public s
我在通过 AJAX 发送 json 时遇到问题。 var data = [{"name": "Will", "surname": "Smith", "age": "40"},{"name": "Wil
当我尝试访问我的 View 中的对象 {{result}} 时(我从 Express js 服务器发送该对象),它只显示 [object][object]有谁知道如何获取 JSON 格式的值吗? 这是
我有不同类型的数据(可能是字符串、整数......)。这是一个简单的例子: public static void main(String[] args) { before("one"); }
嗨,我是 json 和 javascript 的新手。 我在这个网站找到了使用json数据作为表格的方法。 我很好奇为什么当我尝试使用 json 数据作为表时,我得到 [Object,Object]
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我听别人说 null == object 比 object == null check 例如: void m1(Object obj ) { if(null == obj) // Is thi
Match 对象 提供了对正则表达式匹配的只读属性的访问。 说明 Match 对象只能通过 RegExp 对象的 Execute 方法来创建,该方法实际上返回了 Match 对象的集合。所有的
Class 对象 使用 Class 语句创建的对象。提供了对类的各种事件的访问。 说明 不允许显式地将一个变量声明为 Class 类型。在 VBScript 的上下文中,“类对象”一词指的是用
Folder 对象 提供对文件夹所有属性的访问。 说明 以下代码举例说明如何获得 Folder 对象并查看它的属性: Function ShowDateCreated(f
File 对象 提供对文件的所有属性的访问。 说明 以下代码举例说明如何获得一个 File 对象并查看它的属性: Function ShowDateCreated(fil
Drive 对象 提供对磁盘驱动器或网络共享的属性的访问。 说明 以下代码举例说明如何使用 Drive 对象访问驱动器的属性: Function ShowFreeSpac
FileSystemObject 对象 提供对计算机文件系统的访问。 说明 以下代码举例说明如何使用 FileSystemObject 对象返回一个 TextStream 对象,此对象可以被读
我是 javascript OOP 的新手,我认为这是一个相对基本的问题,但我无法通过搜索网络找到任何帮助。我是否遗漏了什么,或者我只是以错误的方式解决了这个问题? 这是我的示例代码: functio
我可以很容易地创造出很多不同的对象。例如像这样: var myObject = { myFunction: function () { return ""; } };
function Person(fname, lname) { this.fname = fname, this.lname = lname, this.getName = function()
任何人都可以向我解释为什么下面的代码给出 (object, Object) 吗? (console.log(dope) 给出了它应该的内容,但在 JSON.stringify 和 JSON.parse
我正在尝试完成散点图 exercise来自免费代码营。然而,我现在只自己学习了 d3 几个小时,在遵循 lynda.com 的教程后,我一直在尝试确定如何在工具提示中显示特定数据。 This code
我是一名优秀的程序员,十分优秀!