C# 解析xml和Json字符串

1.  解析xml文件

例如:XML 数据格式如下:

 
   
   
   
   
 

 
 
   
   
   
   
 



 

用C#代码解析xml字符串数据,先通过数据服务读取到xml字符串,然后通过以下代码解析

public void readXML(string xmlContent) 

{

            if (xmlContent.StartsWith("             {

               //通过得到的xml字符串转换成xml格式,然后进行解析
                XDocument categoriesXML = XDocument.Load(XmlReader.Create(new StringReader(xmlContent)));         


                XElement datas = categoriesXML.Element("RelatedDataItem");

                foreach (XElement WellElement in datas.Elements())
                {

                    if (WellElement.Name == "DataItem")
                    {
                        string wellLogType = WellElement.Attribute("key").Value;

                        string dataService = WellElement.Element("DataService").Attribute("Value").Value;
                        string method = WellElement.Element("Method").Attribute("Value").Value;
                        string dataSetID = WellElement.Element("DataSetID").Attribute("Value").Value;
                        string contentType = WellElement.Element("ContentType").Attribute("Value").Value;

                       
                    }
                }
          }

          else

          {

                throw new exceptio("xml数据格式不正确");

         }

}

 

2. 解析json字符串

例如: Json数据格式如下

   "point1": [
                {
                    "ptId": "1",
                    "X": "22",
                    "Y": "22",
                    "Z": "22"
                },
                {
                    "ptId": "2",
                    "X": "33",
                    "Y": "33",
                    "Z": "33"
                },
                {
                    "ptId": "3",
                    "X": "44",
                    "Y": "44",
                    "Z": "44"
                },
                {
                   "ptId": "4",
                    "X": "55",
                    "Y": "55",
                    "Z": "55
                  }
                ],
            "point2": [
                {
                    "ptId": "22",
                    "X": "333",
                    "Y": "333",
                    "Z": "33"3
                },
                {
                   "ptId": "33",
                    "X": "444",
                    "Y": "444",
                    "Z": "444"
                },
                {
                    "ptId": "44",
                    "X": "555",
                    "Y": "555",
                    "Z": "555"
                }
            ]

用C#代码解析Json字符串。通过服务获取到json数据的字符串,然后进行解析

首先得引入第三方的解析JSON的库(也可以用vs自带的解析JSON的库)

第三方库如下:

using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using Newtonsoft.Json.Serialization;
using Newtonsoft.Json.Linq;

 

   public void test(ScriptObject e)
   {

            if (e == null)
                return;

            
            for (int pointCount= 0; pointCount>= 0; pointCount++)
            {
                ScriptObject point= (ScriptObject)e.GetProperty("Line" + (point+ 1));    //读取一个点

                if (point== null)
                    break;

                
                for (int i = 0; i >= 0; i++)    //读取点中的每个元素
                {
                    ScriptObject tmpPoint= (ScriptObject)point.GetProperty(i);

                    if (tmpPoint== null || tmpPoint.GetProperty("X").ToString().Trim().Length == 0 || tmpPoint.GetProperty("Y").ToString().Trim().Length == 0)
                        break;

                   

                    stirng tmpID = (string)tmpPoint.GetProperty("ptId");

                    float X = float.Parse((tmpPoint.GetProperty("X").ToString()));
                    float Y = float.Parse((tmpPoint.GetProperty("Y").ToString()));
                      float  Z = float.Parse((tmpPoint.GetProperty("Z").ToString()));
                }
            } 
      }

利用 VS 自带的JSON解析库进行解析,大概代码如下

引入库

using System.Json;

 

public void test(string jsonString)

{

      JsonObject jo = (JsonObject)JsonObject.Parse(jsonString);

      JsonArray tmpObj = jo["point1"];

 

     object objId = tmpObj["ptId"];

     object objX = tmpObj["X"];

     object objY = tmpObj["Y"];

     object objZ = tmpObj["Z"];

}

 

 

 

 

 

 

你可能感兴趣的:(编程语言)