ASP.NET日常开发随手记------解析和转换xml

public static ANData ParseString(string str)
{
if (string.IsNullOrEmpty(str))
return null;

XElement x;
try { x = XElement.Parse(str); }
catch { return null; }

if (x.Name != "AN")
return null;

XAttribute ver = x.Attribute("Version");
if (ver == null || ver.Value != "1")
return null;

return new ANData
{
PTN = x.Descendants("PTN").FirstOrDefault().GetValue().ToInt32(),
SDH = x.Descendants("SDH").FirstOrDefault().GetValue().ToInt32(),
ONU = x.Descendants("ONU").FirstOrDefault().GetValue().ToInt32(),
PBX = x.Descendants("PBX").FirstOrDefault().GetValue().ToInt32(),
IPPBX = x.Descendants("IPPBX").FirstOrDefault().GetValue().ToInt32(),
Other = x.Descendants("Other").FirstOrDefault().GetValue().ToInt32(),
BuildPipNumber = x.Descendants("BuildPipNumber").FirstOrDefault().GetValue().ToDecimal(),
BuildBarNumber = x.Descendants("BuildBarNumber").FirstOrDefault().GetValue().ToDecimal(),
BuildCableNumber = x.Descendants("BuildCableNumber").FirstOrDefault().GetValue().ToDecimal(),
BuildLineNumber = x.Descendants("BuildLineNumber").FirstOrDefault().GetValue().ToDecimal(),
Cost = x.Descendants("Cost").FirstOrDefault().GetValue().ToDecimal(),
DevelopmentCost = x.Descendants("EquipmentPrice").FirstOrDefault().GetValue().ToDecimal(),
PipPrice = x.Descendants("PipPrice").FirstOrDefault().GetValue().ToDecimal(),
BarPrice = x.Descendants("BarPrice").FirstOrDefault().GetValue().ToDecimal(),
CablePrice = x.Descendants("CablePrice").FirstOrDefault().GetValue().ToDecimal(),
DesignFee = x.Descendants("DesignPrice").FirstOrDefault().GetValue().ToDecimal().IfNotNull(s => s.Value, x.Descendants("DesignFee").FirstOrDefault().GetValue().ToDecimal()),
InstallationCost = x.Descendants("WorkPrice").FirstOrDefault().GetValue().ToDecimal().IfNotNull(s => s.Value, x.Descendants("InstallationCost").FirstOrDefault().GetValue().ToDecimal()),
NoDisCountInstallationCost = x.Descendants("NoDisCountInstallationCost").FirstOrDefault().GetValue().ToDecimal().IfNotNull(s => s.Value, x.Descendants("InstallationCost").FirstOrDefault().GetValue().ToDecimal()),
SafeCost = x.Descendants("SafeCost").FirstOrDefault().GetValue().ToDecimal(),
SupervisionFee = x.Descendants("ManagePrice").FirstOrDefault().GetValue().ToDecimal().IfNotNull(s => s.Value, x.Descendants("SupervisionFee").FirstOrDefault().GetValue().ToDecimal()),
ReserveFee = x.Descendants("ReserveFee").FirstOrDefault().GetValue().ToDecimal(),
};
}


public override string ToString()
{
XElement x = new XElement("AN");
x.SetAttributeValue("Version", "1");
x.Add(
new XElement("PTN", PTN)
, new XElement("SDH", SDH)
, new XElement("ONU", ONU)
, new XElement("PBX", PBX)
, new XElement("IPPBX", IPPBX)
, new XElement("Other", Other)
, new XElement("BuildPipNumber", BuildPipNumber)
, new XElement("BuildBarNumber", BuildBarNumber)
, new XElement("BuildCableNumber", BuildCableNumber)
, new XElement("BuildLineNumber", BuildLineNumber)
, new XElement("Cost", Cost)
, new XElement("EquipmentPrice", DevelopmentCost)
, new XElement("PipPrice", PipPrice)
, new XElement("BarPrice", BarPrice)
, new XElement("CablePrice", CablePrice)
, new XElement("DesignPrice", DesignFee)
, new XElement("WorkPrice", InstallationCost)
, new XElement("NoDisCountInstallationCost", NoDisCountInstallationCost)
, new XElement("SafeCost", SafeCost)
, new XElement("ManagePrice", SupervisionFee)
, new XElement("ReserveFee", ReserveFee)
);

return x.ToString();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焦糖丨玛奇朵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值