【问题标题】:one complex business XML to Object deserialize in Silverlight to rule them all一个复杂的业务 XML 到 Silverlight 中的对象反序列化以统治它们
【发布时间】:2025-12-06 11:00:01
【问题描述】:

我现在一直在寻找一些教程并发布如何将复杂的 XML 文档反序列化为对象并在 silverlight 应用程序中使用它。我看过很多 2008 年、2009 年的教程和帖子,其中很多显然不起作用。使用 Hello World 或“Person”类有点容易,但我找不到关于一些复杂 xml 文件的任何好东西和对数据的反对。 3行解析代码不是问题,但是c#代码是什么样子的呢?我如何创建所有这些类?如何处理这些对象?

让我们想象一下,您从 Web 服务中获得了一个流,或者您的逻辑中有一个字符串,就像这样。你有命名空间、属性和 6 个子节点。

<?xml version="1.0" encoding="UTF-8"?>
<om:root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns:om="http://www.opengis.net/om/1.0">
<a name="a1">
<b>
    <c id="1" name="C1">
        <d id="1" name="D1">My name is D1</d>
        <d id="2" name ="D2">My name is D2</d>
        <e>
            <f>this is f1</f>
        </e>
    </c>
    <c id="2" name="C2">
        <d id="3" name="D3">My name is D3</d>
        <e>
            <f>this is f2</f>
        </e>
    </c>
</b>
<b>
    <c id="3" name="C3">
        <d id="4" name="D5">My name is D4</d>
        <d id="5" name="D5">My name is D5</d>
        <d id="6" name="D6">My name is D6</d>
        <d id="7" name="D7">My name is D7</d>
        <e>
            <f>this is f3</f>
        </e>
    </c>
</b>
</a>
</om:root>

1) 是否有任何网站可以创建类和 c# 代码以便能够在 xml 中工作/存储数据?您发布您的 xml 文件并获得您的 c# 类的源代码。像 www.json2csharp.com 之类的东西

2) 我的类、属性等会是什么样子?我想看看一种标准的现代最先进的方式如何处理属性、属性、列表等。带有“使用 xxx”的完整代码!请让我们使用可与 Silverlight 一起使用的标准序列化程序类!

3) 一些对象动作。喜欢

  1. 给我一个列表中的所有 nom:c
  2. 给我 f in nom:c id="2" 的值
  3. 将所有 b 放入一个列表中
  4. 给我所有来自 name="" FROM all test:d 个节点的属性值

只是一些实用的查询,而不是那些简单模式的 get.me.person.name 或 book.name

edit:好的,也许这太难了。任何人都可以发布一些链接,其中至少有一种教程或带有一些示例的博客?

【问题讨论】:

  • 这很难还是不是一个常见的问题??

标签: c# silverlight serialization xml-serialization deserialization


【解决方案1】:

我自己是一个初学者,但如果我是你,我肯定会研究 System.Xml.Linq 类。当你已经弄清楚你的类时,反序列化你的文档应该很简单。在下面,我从我的代码中粘贴了一个 sn-p,它完全符合您的要求。它适用于 WP7,但除了独立存储部分之外,一切都应该是相同的。

ItemTemplate item = new ItemTemplate();
        using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("/Items.xml", FileMode.Open))
            {
                XElement doc = XElement.Load(stream);
                IEnumerable<XElement> itemTest = from el in doc.Elements("item")
                                                 where (string)el.Element("id") == itemId
                                                 select el;
                foreach (XElement el in itemTest)
                {
                    item.ItemContent = el.Element("content").Value;
                    item.Status = Convert.ToBoolean(el.Element("status").Value);
                    item.Notes = el.Element("notes").Value;
                    item.Location = el.Element("location").Value;
                    item.Deadline = Convert.ToDateTime(el.Element("deadline").Value);
                }                  
            }
        }

【讨论】:

  • 嗨,谢谢你的代码,但我不想在这里使用 LINQ。只是类和反序列化。
【解决方案2】:

要从 xml 生成 c# 类,您可以使用 Microsoft 工具 xsd.exe

【讨论】:

  • 我用过那个,但不是很开心。一些在线解决方案会很棒。
最近更新 更多