XDocument or XmlDocument to JSON with C#

2019-07-04 09:57发布

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"}]

3条回答
相关推荐>>
2楼-- · 2019-07-04 10:35

Use the method call

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

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

查看更多
forever°为你锁心
3楼-- · 2019-07-04 10:40

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();
    }
查看更多
祖国的老花朵
4楼-- · 2019-07-04 10:50

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
查看更多
登录 后发表回答