简体   繁体   English

如何为JObject中的其他属性正确分配属性?

[英]How to correctly assign properties for other properties in JObject?

What I am trying to achieve is converting a JObject to XML Document and then extract the outer xml from the XMl Document. 我想要实现的是将JObject转换为XML文档,然后从XMl文档中提取外部xml。 Reasons behind this is to send the results as a push notification through Azure Notification Hub. 其背后的原因是通过Azure Notification Hub将结果作为推送通知发送。

What I am trying to get is: 我想要得到的是:

<toast>
    <visual>
        <binding template="ToastGeneric">
            <text id="1">Message</text>
        </binding>
    </visual>
</toast> 

What I have tried: 我试过的

JObject notificationPayload = new JObject(
    new JProperty("toast",
        new JObject(
            new JProperty("visual",
                new JObject(
                    new JProperty("binding",
                        new JObject(
                            new JProperty("@template", "ToastGeneric"),
                            new JProperty("text", notificationMessage,
                                new JProperty("@id", "1")))))))));

The above code is throwing an exception: Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray. 上面的代码引发异常: Can not add Newtonsoft.Json.Linq.JProperty to Newtonsoft.Json.Linq.JArray. So what I have tried after is: 所以我尝试过的是:

JObject notificationPayload = new JObject(
    new JProperty("toast",
        new JObject(
            new JProperty("visual",
                new JObject(
                    new JProperty("binding",
                        new JObject(
                            new JProperty("@template", "ToastGeneric"),
                            new JProperty("text", notificationMessage,
                                new JObject(
                                    new JProperty("@id", "1"))))))))));

Above code gave me a result, but not the intended one. 上面的代码给了我结果,但没有达到预期的结果。 What I got was: 我得到的是:

<toast>
    <visual>
        <binding template="ToastGeneric">
            <text>Message</text>
            <text id="1" />
        </binding>
    </visual>
</toast>

To extract Xml From the JObject I am using the following method: 从JObject提取Xml我正在使用以下方法:

string jsonStringToConvertToXmlString = JsonConvert.SerializeObject(notificationPayload);
XmlDocument doc = JsonConvert.DeserializeXmlNode(jsonStringToConvertToXmlString);
return doc.OuterXml;

Question: How could I give the id property to the same Text property? 问题:如何将id属性赋予相同的Text属性?

Basically, don't use a tool oriented at JSON to construct XML. 基本上,不要使用面向JSON的工具来构造XML。 If you already had JSON, it would make sense to use Json.NET to convert it to XML - but as you're building it from scratch, it's much cleaner to use LINQ to XML: 如果您已经有了 JSON,则可以使用Json.NET将其转换为XML是有意义的-但是,当您从头开始构建JSON时,将LINQ转换为XML会更清洁:

XDocument doc = new XDocument(
    new XElement("toast",
        new XElement("visual",
            new XElement("binding",
                new XAttribute("template", "ToastGeneric"),
                new XElement("text",
                    new XAttribute("id", 1),
                    "Message"
                )
            )
        )
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM