使用属性名称中的编码字符反序列化 XMLDocument

发布于 2024-11-19 11:45:34 字数 1038 浏览 1 评论 0原文

我正在尝试使用 C# 将 xml 数据反序列化为对象。我一直使用 .NET 反序列化方法来完成此操作,并且该方法对于我需要的大部分内容都很有效。

现在,我有由 Sharepoint 创建的 XML,并且我需要反序列化的数据的属性名称已编码字符,即:

*space、º、çã、:、* 和连字符 作为 x0020x00bax007ax00e3x003ax002d< /em> 分别

我试图找出必须在属性 XmlAttribute

x0020 中的 attributeName 参数中放入的内容,以便很好地转换为空格,因此,对于例如,我可以用来

[XmlAttribute(AttributeName = "ows_Nome Completo")]

阅读

ows_Nome_x0020_Completo="MARIA..."

另一方面,既不

[XmlAttribute(AttributeName = "ows_Motiva_x00e7__x00e3_o_x003a_")]

[XmlAttribute(AttributeName = "ows_Motivação_x003a_")]

不允许

[XmlAttribute(AttributeName = "ows_Motivação:")]

我阅读

ows_Motiva_x00e7__x00e3_o_x003a_="text to read..."

前两个我没有返回任何值,第三个给我一个无效字符(冒号)的运行时错误。

无论如何,要使其与 .NET Deserialize 一起使用,还是我必须为此构建一个特定的反序列化器?

谢谢!

I'm Trying to deserialize xml data into an object with c#. I have always done this using the .NET deserialize method, and that has worked well for most of what I have needed.

Now though, I have XML that is created by Sharepoint and the attribute names of the data I need to deserialize have encoded caracters, namely:

*space, º, ç ã, :, * and a hyphen as
x0020, x00ba, x007a, x00e3, x003a and x002d respectivly

I'm trying to figure out what I have to put in the attributeName parameter in the properties XmlAttribute

x0020 converts to a space well, so, for instance, I can use

[XmlAttribute(AttributeName = "ows_Nome Completo")]

to read

ows_Nome_x0020_Completo="MARIA..."

On The other hand, neither

[XmlAttribute(AttributeName = "ows_Motiva_x00e7__x00e3_o_x003a_")]

nor

[XmlAttribute(AttributeName = "ows_Motivação_x003a_")]

nor

[XmlAttribute(AttributeName = "ows_Motivação:")]

allow me to read

ows_Motiva_x00e7__x00e3_o_x003a_="text to read..."

With the first two I get no value returned, and the third gives me a runtime error for invalid caracters (the colon).

Anyway to get this working with .NET Deserialize, or do I have to build a specific deserializer for this?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

榆西 2024-11-26 11:45:34

您所看到的(“神秘”数据)称为 XML 实体。 SharePoint 使用它来保存属性名称和类似元素。

有几种方法可以处理这个问题,最优雅的解决方法是提取列表模式并将元素与该模式进行匹配。该架构包含有关列表数据的所有元数据。可以在下面或此处看到架构的精美示例 http://www.bendsoft.com/documentation/camelot-php-tools/1_5/packets/schema-and-content-packets/schemas/example-list-view-schema/

如果您不这样做不想走那条路,你可以从这里开始 http://msdn.microsoft.com/en-us/library/35577sxd.aspx

<Field Name="ContentType">
  <ID>c042a256-787d-4a6f-8a8a-cf6ab767f12d</ID>
  <DisplayName>Content Type</DisplayName>
  <Type>Text</Type>
  <Required>False</Required>
  <ReadOnly>True</ReadOnly>
  <PrimaryKey>False</PrimaryKey>
  <Percentage>False</Percentage>
  <RichText>False</RichText>
  <VisibleInView>True</VisibleInView>
  <AppendOnly>False</AppendOnly>
  <FillInChoice>False</FillInChoice>
  <HTMLEncode>False</HTMLEncode>
  <Mult>False</Mult>
  <Filterable>True</Filterable>
  <Sortable>True</Sortable>
  <Group>_Hidden</Group>
</Field>
<Field Name="Title">
  <ID>fa564e0f-0c70-4ab9-b863-0177e6ddd247</ID>
  <DisplayName>Title</DisplayName>
  <Type>Text</Type>
  <Required>True</Required>
  <ReadOnly>False</ReadOnly>
  <PrimaryKey>False</PrimaryKey>
  <Percentage>False</Percentage>
  <RichText>False</RichText>
  <VisibleInView>True</VisibleInView>
  <AppendOnly>False</AppendOnly>
  <FillInChoice>False</FillInChoice>
  <HTMLEncode>False</HTMLEncode>
  <Mult>False</Mult>
  <Filterable>True</Filterable>
  <Sortable>True</Sortable>
</Field>
<Field>
   ...
</Field>

What you are looking at (the "cryptic" data) is called XML entities. It's used by SharePoint to safekeep attribute names and similar elements.

There are a few ways of dealing with this, the most elegant ways to solve it is by extracting the List schema and match the element towards the schema. The schema contain all meta-data about your list data. A polished example of a Schema can be seen below or here http://www.bendsoft.com/documentation/camelot-php-tools/1_5/packets/schema-and-content-packets/schemas/example-list-view-schema/

If you don't want to walk that path you could start here http://msdn.microsoft.com/en-us/library/35577sxd.aspx

<Field Name="ContentType">
  <ID>c042a256-787d-4a6f-8a8a-cf6ab767f12d</ID>
  <DisplayName>Content Type</DisplayName>
  <Type>Text</Type>
  <Required>False</Required>
  <ReadOnly>True</ReadOnly>
  <PrimaryKey>False</PrimaryKey>
  <Percentage>False</Percentage>
  <RichText>False</RichText>
  <VisibleInView>True</VisibleInView>
  <AppendOnly>False</AppendOnly>
  <FillInChoice>False</FillInChoice>
  <HTMLEncode>False</HTMLEncode>
  <Mult>False</Mult>
  <Filterable>True</Filterable>
  <Sortable>True</Sortable>
  <Group>_Hidden</Group>
</Field>
<Field Name="Title">
  <ID>fa564e0f-0c70-4ab9-b863-0177e6ddd247</ID>
  <DisplayName>Title</DisplayName>
  <Type>Text</Type>
  <Required>True</Required>
  <ReadOnly>False</ReadOnly>
  <PrimaryKey>False</PrimaryKey>
  <Percentage>False</Percentage>
  <RichText>False</RichText>
  <VisibleInView>True</VisibleInView>
  <AppendOnly>False</AppendOnly>
  <FillInChoice>False</FillInChoice>
  <HTMLEncode>False</HTMLEncode>
  <Mult>False</Mult>
  <Filterable>True</Filterable>
  <Sortable>True</Sortable>
</Field>
<Field>
   ...
</Field>
乙白 2024-11-26 11:45:34

嗯……我想我已经找到了一种方法,目前有效。只是替换了 _x***_ 字符,并相应地更正了 XmlAttributes。通过首先将 xml 作为字符串加载,然后进行替换,然后将“干净”文本作为 XML 加载来完成此替换。

但我仍然想知道是否可以使用一些 XmlAttribute Name 来实现更直接的方法......

Well... I guess I kind of hacked a way around, which works for now. Just replaced the _x***_ charecters for nothing, and corrected the XmlAttributes acordingly. This replacement is done by first loading the xml as a string, then replacing, then loading the "clean" text as XML.

But I wopuld still like to know if it is possible to use some XmlAttribute Name for a more direct approach...

我喜欢麦丽素 2024-11-26 11:45:34

尝试使用 System.Xml; XmlConvert.EncodeNameXmlConvert.DecodeName

Try using System.Xml; XmlConvert.EncodeName and XmlConvert.DecodeName

半透明的墙 2024-11-26 11:45:34

我使用一个简单的函数来获取 NameCol:

private string getNameCol(string colName) {
    if (colName.Length > 20) colName = colName.Substring(0, 20);

    return System.Xml.XmlConvert.EncodeName(colName);
}

我已经在搜索 á、é、í、ó、ú 等替换字符。 EncodeName 不会转换此字符。
可以使用替换:

.Replace("ó","_x00f3_").Replace("á","_x00e1_")

I use a simply function to get the NameCol:

private string getNameCol(string colName) {
    if (colName.Length > 20) colName = colName.Substring(0, 20);

    return System.Xml.XmlConvert.EncodeName(colName);
}

I'm already searching for replace characters like á, é, í, ó, ú. EncodeName doesn't convert this characters.
Can use Replace:

.Replace("ó","_x00f3_").Replace("á","_x00e1_")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文