• xml 获取节点名称 和指定节点的值


    string path = Server.MapPath("~/Models");//路径

    doc.Load(path + "/CourierCompany.xml");//加载xml

    XmlNode rootNode = doc.DocumentElement;

    for (int i = 0; i < rootNode.ChildNodes.Count; i++)
    {

    var name= rootNode.ChildNodes[i].Name;//获取节点名称

    var value= rootNode.SelectSingleNode(rootNode.ChildNodes[i].Name).FirstChild.InnerText;//获取指定节点的值

    }

    例子如下;

    public JsonResult GetLogisticList()
    {

    string path = Server.MapPath("~/Models");
    //XDocument document = XDocument.Load(path + "/CourierCompany.xml");
    //获取到XML的根元素进行操作

    List<CourierStatus> couriers = new List<CourierStatus>();
    XmlDocument doc = new XmlDocument();

    doc.Load(path + "/CourierCompany.xml");
    XmlNode rootNode = doc.DocumentElement;
    for (int i = 0; i < rootNode.ChildNodes.Count; i++)
    {
    CourierStatus status = new CourierStatus();
    status.Name = rootNode.ChildNodes[i].Name;//获取节点名称
    status.VName= rootNode.SelectSingleNode(rootNode.ChildNodes[i].Name).FirstChild.InnerText;//获取指定节点的值
    couriers.Add(status);
    }
    return Json(couriers);
    }

    public class CourierStatus
    {
    public string Name { get; set; }
    public string VName { get; set; }
    }

    xml文件内容

    <couriers>
    <SF>顺丰速运</SF>
    <HTKY>百世快递</HTKY>
    <ZTO>中通快递</ZTO>
    <STO>申通快递</STO>
    <YTO>圆通速递</YTO>
    <YD>韵达速递</YD>
    <YZPY>邮政快递包裹</YZPY>
    <EMS>EMS</EMS>
    <HHTT>天天快递</HHTT>
    <JD>京东物流</JD>
    <QFKD>全峰快递</QFKD>
    <GTO>国通快递</GTO>
    <UC>优速快递</UC>
    <DBL>德邦</DBL>
    <FAST>快捷快递</FAST>
    <ZJS>宅急送</ZJS>
    </couriers>

    请求结果:

    [
    {
    "Name": "SF",
    "VName": "顺丰速运"
    },
    {
    "Name": "HTKY",
    "VName": "百世快递"
    },
    {
    "Name": "ZTO",
    "VName": "中通快递"
    },
    {
    "Name": "STO",
    "VName": "申通快递"
    },
    {
    "Name": "YTO",
    "VName": "圆通速递"
    },
    {
    "Name": "YD",
    "VName": "韵达速递"
    },
    {
    "Name": "YZPY",
    "VName": "邮政快递包裹"
    },
    {
    "Name": "EMS",
    "VName": "EMS"
    },
    {
    "Name": "HHTT",
    "VName": "天天快递"
    },
    {
    "Name": "JD",
    "VName": "京东物流"
    },
    {
    "Name": "QFKD",
    "VName": "全峰快递"
    },
    {
    "Name": "GTO",
    "VName": "国通快递"
    },
    {
    "Name": "UC",
    "VName": "优速快递"
    },
    {
    "Name": "DBL",
    "VName": "德邦"
    },
    {
    "Name": "FAST",
    "VName": "快捷快递"
    },
    {
    "Name": "ZJS",
    "VName": "宅急送"
    }
    ]

    不当之处; 请指教

  • 相关阅读:
    活动设计的“七宗罪”(转)
    BAYESIAN STATISTICS AND CLINICAL TRIAL CONCLUSIONS: WHY THE OPTIMSE STUDY SHOULD BE CONSIDERED POSITIVE(转)
    iOS开发—— UIImagePickerController获取相册和拍照
    iOS开发——UIImageView
    iOS开发——导入第三方库引起的unknown type name 'NSString'
    iOS开发——UITableView(未完,待续...)
    iOS开发——Reachability和AFNetworking判断网络连接状态
    iOS开发——GCDAsyncSocket
    iOS开发——pch文件创建
    iOS开发——打开手机相册,获取图片
  • 原文地址:https://www.cnblogs.com/wwr01/p/10573818.html
Copyright © 2020-2023  润新知