You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

XML反序列化异常:无法读取LandXML中linearUnit属性值

解决LandXML反序列化无法获取linearUnit值的问题

问题场景

尝试反序列化LandXML文件,期望输出其中linearUnit的"meter"值到控制台,程序无报错但未输出目标值。相关代码及XML片段如下:

XML文件片段

<?xml version="1.0" encoding="utf-8"?>
<LandXML xmlns="http://www.landxml.org/schema/LandXML-1.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.landxml.org/schema/LandXML-1.2 http://www.landxml.org/schema/LandXML-1.2/LandXML-1.2.xsd" version="1.2" date="2022-06-22" time="16:31:46.94" readOnly="false" language="English">
    <Units>
        <Metric linearUnit="meter" areaUnit="squareMeter" volumeUnit="cubicMeter" angularUnit="decimal dd.mm.ss" latLongAngularUnit="decimal degrees" temperatureUnit="celsius" pressureUnit="milliBars"/>
    </Units>
...

原C#实体类

using System;
using System.Xml.Serialization;

namespace MLS.SpatialViz.Hexagon
{
    [Serializable]
    [XmlRoot(ElementName = "LandXML", IsNullable = true)]

    public class LandXML
    {
        [XmlElement(ElementName = "Units")]
        public Units Units { get; set; }

    }

    public class Units
    {
        [XmlAttribute(AttributeName = "linearUnit")]
        public string LinearUnit { get; set; }

    }
}

原反序列化代码

public static void DeserealizeXML()
{
    string FileName = @"D:\Temp\824ABF00.xml";
    
    XmlSerializer serializer = new XmlSerializer(typeof(Hexagon.LandXML));

    Hexagon.LandXML landXML;

    using (XmlTextReader xmlReader = new XmlTextReader(FileName))
    {
        xmlReader.Namespaces = false;
        xmlReader.WhitespaceHandling = WhitespaceHandling.None;

        landXML = (Hexagon.LandXML)serializer.Deserialize(xmlReader);
    }

    Console.WriteLine($"LinearUnit: {landXML.Units.LinearUnit}");
    Console.ReadKey();
}

问题原因

  1. 未处理XML命名空间:XML根元素LandXML包含默认命名空间http://www.landxml.org/schema/LandXML-1.2,原实体类未指定该命名空间,导致XmlSerializer无法正确匹配元素。
  2. 实体类结构不匹配:linearUnit是<Metric>元素的属性,而非<Units>元素的属性,原Units类直接将LinearUnit映射为linearUnit属性,结构与XML层级不符。

修正方案

步骤1:修正实体类结构并添加命名空间

using System;
using System.Xml.Serialization;

namespace MLS.SpatialViz.Hexagon
{
    [Serializable]
    // 指定XML根元素的命名空间,与XML文件中的默认命名空间一致
    [XmlRoot(ElementName = "LandXML", Namespace = "http://www.landxml.org/schema/LandXML-1.2", IsNullable = true)]
    public class LandXML
    {
        // 指定Units元素的命名空间
        [XmlElement(ElementName = "Units", Namespace = "http://www.landxml.org/schema/LandXML-1.2")]
        public Units Units { get; set; }
    }

    public class Units
    {
        // 添加Metric子元素的映射,并指定命名空间
        [XmlElement(ElementName = "Metric", Namespace = "http://www.landxml.org/schema/LandXML-1.2")]
        public Metric Metric { get; set; }
    }

    public class Metric
    {
        // 映射linearUnit属性
        [XmlAttribute(AttributeName = "linearUnit")]
        public string LinearUnit { get; set; }
        
        // 可选:如果需要其他属性,可以在这里添加,比如areaUnit等
        [XmlAttribute(AttributeName = "areaUnit")]
        public string AreaUnit { get; set; }
    }
}

步骤2:调整反序列化代码(移除xmlReader.Namespaces = false)

public static void DeserealizeXML()
{
    string FileName = @"D:\Temp\824ABF00.xml";
    
    // 由于实体类已指定命名空间,直接使用XmlSerializer即可
    XmlSerializer serializer = new XmlSerializer(typeof(Hexagon.LandXML));

    Hexagon.LandXML landXML;

    using (XmlTextReader xmlReader = new XmlTextReader(FileName))
    {
        // 移除xmlReader.Namespaces = false,否则会忽略命名空间导致匹配失败
        xmlReader.WhitespaceHandling = WhitespaceHandling.None;

        landXML = (Hexagon.LandXML)serializer.Deserialize(xmlReader);
    }

    // 注意层级变化:从landXML.Units.LinearUnit改为landXML.Units.Metric.LinearUnit
    Console.WriteLine($"LinearUnit: {landXML.Units.Metric.LinearUnit}");
    Console.ReadKey();
}

说明

  • 实体类的层级必须与XML严格对应:LandXML → Units → Metric,linearUnit是Metric的属性。
  • 必须为所有需要匹配的元素指定正确的命名空间,否则XmlSerializer无法识别元素。
  • 移除xmlReader.Namespaces = false,该设置会让阅读器忽略XML中的命名空间,导致与实体类的命名空间不匹配。

内容的提问来源于stack exchange,提问作者SteveJ71

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.27 19:15:05