XDocument or XmlDocument to JSON with C#

喜你入骨 提交于 2019-12-10 22:05:08

问题


I have this XML which is great:

<Products>
  <Product ProductCode="C1010" CategoryName="Coins" />
  <Product ProductCode="C1012" CategoryName="Coins" />
  <Product ProductCode="C1013" CategoryName="Coins" />
</Products>

but it outputs to this JSON:

{"Products":{"Product":[{"@ProductCode":"C1010","@CategoryName":"Coins"},
                        {"@ProductCode":"C1012","@CategoryName":"Coins"},     
                        {"@ProductCode":"C1013","@CategoryName":"Coins"}]}}

I would like no 'Product' sublevel in my json because all three lines are a product. This is my C# code:

//x is an XDocument. 
JsonConvert.SerializeXNode(x, Formatting.None, false)
//JsonConvert.SerializeXNode(x); //I also tried without the formatting and the boolean. 

When I 'convert' an XDocument to XmlDocument and use:

var xmlDocument = new System.Xml.XmlDocument();
using (var xmlReader = x.CreateReader())
{
    xmlDocument.Load(xmlReader);
}
JsonConvert.SerializeXmNode(xmlDocument);

It gives me exactly the same output. So how can I modify my JSON parsing such that I have a simple list of products. I prefer the cleanest solution.

To be perhaps a bit more clear, I'd something like this as output:

[{"@ProductCode":"C1010","@CategoryName":"Coins"},
{"@ProductCode":"C1012","@CategoryName":"Coins"},     
{"@ProductCode":"C1013","@CategoryName":"Coins"}]

回答1:


Use the method call

JsonConvert.SerializeXNode(x, Formatting.None, true);

this will omit the root node and should create what you expect.




回答2:


Instread of using xml writer or con version Just read from the original xml file an write in a new file use file stream writer.

basically you would be:

List xml_retrevedData = new List();
FileStramWriter fr = new FileStramWriter(); 
fr.Write("{"Products":[{"@ProductCode":" //colection item 
variable1.data1","@CategoryName":"//colection item 
variable1.data2"}, {"@ProductCode":"//colection item 
variable2.data1","@CategoryName":"//colection item 
variable11.data1"},
{"@ProductCode":"//colection item variable13.data1","@CategoryName":"//colection item variable13.data3"}]}"); 
// in side the file stream Writer



回答3:


try this

public string getData(ref XmlDocument doc) {

        JObject productobj = new JObject();

        var productsenum = from p in doc.GetElementsByTagName("product").Cast<XmlElement>()
                           select p;

        JArray products = new JArray();
        foreach (XmlElement p in productsenum) {
            JObject pobj = new JObject();
            pobj["ProductCode"] = p.GetAttribute("ProductCode");
            pobj["CategoryName"]= p.GetAttribute("CategoryName");

            products.Add(pobj);
        }

        JObject product = new JObject();
        product["Product"] = products;

        productobj["Products"] = product;

        return productobj.ToString();
    }


来源:https://stackoverflow.com/questions/26605493/xdocument-or-xmldocument-to-json-with-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!