F# 为什么会有空名单
我正在编写一个读取xml文件并添加到列表中的应用程序。代码如下所示:F# 为什么会有空名单,f#,F#,我正在编写一个读取xml文件并添加到列表中的应用程序。代码如下所示: open System.Net open System.Collections.Generic open System.Xml.Linq open System type IpRanges = {ipStart: IPAddress; ipEnd: IPAddress; subnet: IPAddress; mask: IPAddress} type System.String with member s1.isE
open System.Net
open System.Collections.Generic
open System.Xml.Linq
open System
type IpRanges = {ipStart: IPAddress; ipEnd: IPAddress; subnet: IPAddress; mask: IPAddress}
type System.String with
member s1.isEqual(s2: string) =
System.String.Equals(s1, s2, System.StringComparison.CurrentCultureIgnoreCase)
let xname name = XName.Get name
let xattr (elem: XElement) (name:string) = elem.Attribute(xname name).Value
let loc (filename:string) (location:string) =
query {
for doc in XDocument.Load(filename).Descendants(xname "location") do
where ((xattr doc "name").isEqual(location))
select doc
}
let extractAddr (doc:seq<XElement>) =
let mutable startAddr:IPAddress = null
let mutable endAddr:IPAddress = null
let mutable subnet:IPAddress = null
let list:List<IpRanges> = new List<IpRanges>()
//let mutable mask = "0.0.0.0"
let parseIP (addr:string) = IPAddress.Parse addr
for e in doc do
match e.Name.LocalName with
| "start" -> startAddr <- parseIP e.Value
| "end" -> endAddr <- parseIP e.Value
| "subnet" -> subnet <- parseIP e.Value
| "mask" -> list.Add({ipStart = startAddr; ipEnd = endAddr; subnet = subnet; mask = parseIP(e.Value)}) |> ignore
| _ -> ()
// Return list
list
[<EntryPoint>]
let main argv =
let location = (loc ("C:\Temp\file.xml") "location").Descendants(xname "range").Descendants()
|> extractAddr
//let list = extractAddr location
Console.ReadLine() |> ignore
0 // return an integer exit code
不会添加到列表中。
Xml结构
192.20.10.1
192.20.10.255
255.255.255.0
192.20.16.1
附属的
192.20.12.1
192.20.12.255
255.255.255.0
192.20.16.1
XML中没有“mask”元素,因此此模式永远不会匹配。
另外,我会将您的extractAddr
函数更改为类似这样的“XML结构证明”(如果没有名为elmname
的元素,则不会引发异常):
让parseRangeElement(范围:XElement)elmname=
设e=range.Element(xname-elmname)
如果e为空,则
将IPAddress.TryParse(e.Value)与匹配
|正确,ip->ip
|->null
否则无效
let extractAddr(文件编号:seq)=
医生
|>Seq.map(有趣的rngelm->
{
ipStart=parseRangeElement rngelm“开始”
ipEnd=parseRangeElement rngelm“结束”
子网=parseRangeElement rngelm“子网”
掩码=parseRangeElement rngelm“网关”
})
|>List.ofSeq//如果您确实需要列表
[]
让主argv=
let location=(loc(xml)“test”).子体(xname“range”)
|>提取器地址
Console.ReadLine()|>忽略
0//返回整数退出代码
您能给出一个正在使用的xml文件示例吗?也许只是片段XML中没有“mask”元素
let list:List<IpRanges> = new List<IpRanges>()
| "mask" -> list.Add({ipStart = startAddr; ipEnd = endAddr; subnet = subnet; mask = parseIP(e.Value)}) |> ignore
let parseRangeElement (range: XElement) elmname =
let e = range.Element(xname elmname)
if e <> null then
match IPAddress.TryParse(e.Value) with
| true, ip -> ip
| _ -> null
else null
let extractAddr (doc:seq<XElement>) =
doc
|> Seq.map (fun rngelm ->
{
ipStart = parseRangeElement rngelm "start"
ipEnd = parseRangeElement rngelm "end"
subnet = parseRangeElement rngelm "subnet"
mask = parseRangeElement rngelm "gateway"
})
|> List.ofSeq // if you really need list
[<EntryPoint>]
let main argv =
let location = (loc (xml) "test").Descendants(xname "range")
|> extractAddr
Console.ReadLine() |> ignore
0 // return an integer exit code