使用C#代码解析给定结构 [英] Parse below given structure using C# code

查看:103
本文介绍了使用C#代码解析给定结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的服务返回给定结构下面



 {
描述:通知,
ID:465e24fc-eeed-58fa-a9ce-6b5108002f2e,
主题:sns:us-asia-1:701722846169:fx-ma,
议程: fx-market-rates,
Key:<?xml version = \1.0 \encoding = \UTF-8 \standalone = \yes \? > \ n<引用xmlns:ns2 = \http:// \retrieval = \2016-05-12T02:03:38Z \type = \MARKET \sequence = \\ \\10/95 \topic = \f\> \ n< id> b24bc9a3-e2ab-491e-9399-689bfceb7707< / id> \ n< rate> 1.366000< / rate> \ n< buy> USD< / buy> \ n< sell> BND< / sell> \ n< unit> USD< / unit> \ n< / Quote> \ n,
时间戳:2016-05-12T02:04:29.232Z,
SignatureVersion:1,
签名:FeKQf0JT3dr4N +,
SigningCertURL:https://du.com/Simple,
Uns ubscribeURL:https:// snsc1
}





首先,我想用C#读取所有属性。第二,现在我想解析使用C#保存哪个Key属性的xml。有人可以帮助我在c#中为上面给出的结构编写代码。



我尝试了什么:



首先,我想用C#读取所有属性。第二,现在我想解析使用C#保存哪个Key属性的xml。有人可以帮助我在c#中为上面给出的结构编写代码。

我尝试了json parse然后xml解析使用xdocument,xreader等等...

提前谢谢

John

解决方案

我建​​议从这里开始:如何:序列化和反序列化JSON数据 [ ^ ]



当你要去的时候能够以字符串形式获得 Key 属性/成员,您可以使用 XDocument类 [ ^ ]进一步的xml处理。



对不起,您的问题描述信息不多......


我不确定你是否真的需要直接处理JSON(和XML)。这取决于数据的来源。如果你还在文件/流中生成数据,你当然应该更好地使用数据合约

使用数据合同 [ ^ ],

DataContractSerializer Class(System.Runtime.Serialization) [ ^ ],

DataContractJsonSerializer Class(System .Runtime.Serialization.Json) [ ^ ]。



即使你不生成数据但必须使用第三方生成的一些数据,通常可以设计与所讨论的数据模型匹配的数据契约以及给出的JSON表示的XML的特性。在JSON的情况下,它更有可能成功。



最后,让我们考虑使用数据契约技术的情况对于用XML,JSON表示的模型是不可行的, 或两者。该怎么办?那么,不幸的是,你很可能需要开发特定于数据模型的映射代码(与数据契约相反,完全模型 - 不可知)。



关于JSON,请看我过去的答案:

haw从Cloudant(json文档)获取数据 [ ^ ],

如何将对象类型转换为C#类对象类型 [ ^ ],

如何将多级json数据转换为C#Object? [ ^ ]。



(对不起,在这些答案中,我还在JavaScript中写了关于JSON的内容,但我也建议理解它,即使它与你的问题无关;首先,理解JSON的JavaScript特性是好的,其次,当使用JSON时,其中一个原因是JavaScript总是涉及到。)



处理XML ,.NET FCL提供不同的可能性。这是我的简短概述:

  1. 使用 System.Xml.XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx
  2. 使用类 System.Xml .XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx
  3. 使用类 System.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx http://msdn.microsoft.com/en-us/library/bb387063.aspx
  4. 祝你好运。

    -SA


Hi My service return below given structure

{
  "Description" : "Notification",
  "ID" : "465e24fc-eeed-58fa-a9ce-6b5108002f2e",
  "Topic" : "sns:us-asia-1:701722846169:fx-ma",
  "Agenda" : "fx-market-rates",
  "Key" : "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n<Quote xmlns:ns2=\"http://\" retrieval=\"2016-05-12T02:03:38Z\" type=\"MARKET\" sequence=\"10/95\" topic=\"f\">\n    <id>b24bc9a3-e2ab-491e-9399-689bfceb7707</id>\n    <rate>1.366000</rate>\n    <buy>USD</buy>\n    <sell>BND</sell>\n    <unit>USD</unit>\n</Quote>\n",
  "Timestamp" : "2016-05-12T02:04:29.232Z",
  "SignatureVersion" : "1",
  "Signature" : "FeKQf0JT3dr4N+",
  "SigningCertURL" : "https://du.com/Simple",
  "UnsubscribeURL" : "https://snsc1"
}



First i want to read all attribute using C#. Second, Now I want to parse xml which Key attribute hold using C#. Can some one help me write code in c# for above given structure.

What I have tried:

First i want to read all attribute using C#. Second, Now I want to parse xml which Key attribute hold using C#. Can some one help me write code in c# for above given structure.
I tried json parse then xml parse using xdocument, xreader so on..
Thanks in advance
John

解决方案

I'd recommend to start here: How to: Serialize and Deserialize JSON Data[^]

When you'll be able to get a Key property/member as a string, you can use XDocument class[^] for further xml proccessing.

Sorry, the description of your issue is not much informative...


I'm not sure that you really need to deal with JSON (and XML) directly. It depends on the origin of data. If you also generate data in the file/stream, you certainly should better use Data Contract:
Using Data Contracts[^],
DataContractSerializer Class (System.Runtime.Serialization)[^],
DataContractJsonSerializer Class (System.Runtime.Serialization.Json)[^].

Even if you don't generate the data but have to use some data generated by 3rd party, it's often possible to design the data contract matching the data model in question and the peculiarities of XML of JSON representation given. It's more likely to succeed in case of JSON.

And, finally, let's consider the cases when using data contract technology is not feasible for the model represented in XML, JSON, or both. What to do? Well, then, unfortunately, it's likely that you would have to develop data model-specific mapping code (in contrast to data contract, which is fully model-agnostic).

As to JSON, please see my past answers:
haw to get data from Cloudant (json document)[^],
How To Convert object type to C# class object type[^],
how to conver multi level json data to C# Object?[^].

(Sorry, in these answers, I also write about JSON in JavaScript, but I recommend to understand that, too, even if it is unrelated to your problem; first, it's good to understand the JavaScript nature of JSON, secondly, when JSON is used, one of the reasons is that JavaScript is always involved.)

To handle XML, .NET FCL offers different possibilities. This is my short overview:

  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx.
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx, http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx.
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx, http://msdn.microsoft.com/en-us/library/bb387063.aspx.

Good luck.

—SA


这篇关于使用C#代码解析给定结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆