如何使用不断变化的XML

时间:2011-10-13 20:23:45

标签: c# xml-parsing

情景:

输入:

  • 一个XML文件,其结构不断受外部团队的影响。
  • 这不是一个格式良好的XML。
  • 元素标签有时会改变。

必需输出:

  • 应用程序其他部分使用的预定义类的对象

问题:

  • 如何使用固定的类名映射更改的元素标记。

C#语言。

enter code here
ReadIn("input.xml");

public static GbtInfo ReadIn(string path)
    {
        using (XmlReader reader = new XmlTextReader(path))
        {
            reader.ReadToDescendant("SYSTEM");
            return Serializers.ParseNode<GbtInfo>(reader);
        }
    }

public static T ParseNode<T>(XmlReader reader)
{
    Type t = typeof(T);
    return (T)ParseNode(t, reader);
}

public static object ParseNode(Type type, XmlReader reader)
{
    var instance = Activator.CreateInstance(type);

    IXmlSerializable xmlSerializable = instance as IXmlSerializable;
    if (xmlSerializable != null)
    xmlSerializable.ReadXml(reader);
    return instance;
}

public static object ParseNode(string name_space, string elementName, XmlReader reader)
{
    Type t = Type.GetType(name_space + "." + elementName, false, true);
    return ParseNode(t, reader);
}





public void ReadXml(System.Xml.XmlReader reader)
    {
        this.reader = reader;
        string nextElement;
        parent = reader.Name;
        PropertyInfo propertyinfo = null;

        //Setting a flag if the current node is empty.
        bool isEmptyElement = reader.IsEmptyElement;
        //Code that parses the attributes out of the Node.
        if (reader.HasAttributes)
        {
            for (int i = 0; i < reader.AttributeCount; i++)
            {
                reader.MoveToAttribute(i);
                nextElement = Utilities.RemoveSpecialChar(reader.Name);
                propertyinfo = (GetType()).GetProperty(nextElement, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                if (propertyinfo != null)
                    propertyinfo.SetValue(this, reader.Value, null);
                else
                    PrintError(nextElement);


            }
        }
        if (!isEmptyElement)//if the element is not empty get all the children 
        {
            reader.Read();
            Utilities.SkipToContent(reader);
            while (!(reader.Name.Equals(parent) && reader.NodeType == XmlNodeType.EndElement))
            {
                reader.MoveToContent();
                //Case when Node Element is an object type with string 
                if (reader.NodeType == XmlNodeType.Text)
                {
                    propertyinfo = (GetType()).GetProperty("Value", BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

                    if (propertyinfo != null)
                        propertyinfo.SetValue(this, reader.Value, null);
                    else
                        PrintError("Value");

                    //Testing Console.WriteLine(nextelement + " => " + reader.Value);
                    reader.Read();
                    Utilities.SkipToContent(reader);
                }
                if (reader.NodeType == XmlNodeType.Element)
                {
                    nextElement = Utilities.RemoveSpecialChar(reader.Name);
                    propertyinfo = (GetType()).GetProperty(nextElement, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);
                    if (propertyinfo != null)
                    {
                        if (propertyinfo.PropertyType.FullName.Equals("System.String"))
                        {
                            reader.Read();//read to get the text
                            if (reader.NodeType != XmlNodeType.Text)
                                throw new InvalidOperationException("Special Case encountered check XML");
                            propertyinfo.SetValue(this, reader.Value, null);
                            //Testing Console.WriteLine(reader.Value);
                            reader.ReadToNextSibling("dummy");//this will read to the parent end tag
                            reader.Read();
                            Utilities.SkipToContent(reader);
                        }
                        else
                        {

                            System.Collections.IList list = propertyinfo.GetValue(this, null) as System.Collections.IList;
                            if (list != null)
                            {
                                list.Add(Serializers.ParseNode(Namespace, nextElement, reader));

                            }
                            else
                            {
                                propertyinfo.SetValue(this, Serializers.ParseNode(Namespace, nextElement, reader), null);
                            }
                        }
                    }
                    else
                    {
                        PrintError(nextElement);
                        reader.ReadToNextSibling();
                    }
                }
            }
        }

        //move to the next element
        reader.Read();
        Utilities.SkipToContent(reader);
    }


    // Utilities Method

      private void PrintError(string errorElement)
        {
            IXmlLineInfo info = reader as IXmlLineInfo;
            Log.LogIt("The attribute " + errorElement + " does not exist under " + parent + " Error Occurred at Line "
                      + info.LineNumber + " Col " + info.LinePosition, LogMessageType.Warning);
            info = null;
    }

     public static XmlReader SkipToContent(XmlReader reader)
        {
            int count = 0;
            while (reader.NodeType != XmlNodeType.Element && reader.NodeType != XmlNodeType.Attribute &&
                   reader.NodeType != XmlNodeType.EndElement && reader.NodeType != XmlNodeType.Text)
            {
                reader.Read(); count++;
                if (count > 2)
                {
                    //Console.WriteLine(" Stuck");
                    if (reader.EOF)
                    {
                        break;
                    }
                }
            }
            return reader;
        }

/// <summary>
/// Removes special symbols like "-","_","." from Node element name inorder to match it with the respective objects. 
/// </summary>
/// <param name="str"></param>
/// <returns></returns>
public static string RemoveSpecialChar(string str)
        {
            str = str.Replace("-", "");
            str = str.Replace(".", "");
            str = str.Replace("_", "");
            return str;
    }

1 个答案:

答案 0 :(得分:2)

首先,您应该强制您的外部团队至少为您提供语法正确的XML文件。这是一个组织问题,但如果你无法解决这个问题,其他一切都没有多大意义。

  

应用程序其他部分使用的预定义类的对象

使用XmlDocument。在代码中动态访问内容,并在运行时缺少标记或块时处理这种情况。只要您不知道元素标记在下一次迭代中不再更改,就避免将元素标记映射到C#类中的静态定义属性。如果您的代码依赖于某些特定的元素标记,请在代码的中心位置将它们定义为字符串常量,以便在外部团队重命名标记时轻松更改它们。