1

我有多个 XML 模板,我想使用 LINQ to XML 填充它们,但我不确定如何开始,这意味着如何正确读取 XML 文件(最佳方法)并填充属性和节点(InnerText)。此外,如何在现有模板中创建新元素和属性以及删除/更新现有模板?下面是一个模板示例:

<Person age="age">
<FirstName></FirstName>
<LastName></LastName>
<Children>
<Name></Name>
</Children>
</Person>

在上图中,age 属性是占位符,Children 下的 FirstName、LastName 和 Name 是占位符。我们还假设我想要删除子项下的名称或添加名称。还假设我想在 Person 中添加一个带有日、月、年属性的 DOB 元素?

4

3 回答 3

1

这是一个例子。您为每个模板构建一个类。我正在使用以下库扩展类 (XElementExtensions.cs):https ://github.com/ChuckSavage/XmlLib/

public class Person
{
    XElement self;
    public Person(XElement person) { self = person; }

    // Age should be a value of Now minus DOB in years.
    public int Age { get { return DateTime.Now.Year - DOB.Year; } }

    public DateTime DOB
    {
        get { return self.Get("DOB", DateTime.MinValue ); } // choose a default date that works for you
        set { self.Set("DOB", value, true); } // true set as attribute
    }

    public string FirstName
    {
        get { return self.Get("FirstName", string.Empty); }
        set { self.Set("FirstName", value, false); } // false set as child node
    } 

    public string LastName
    {
        get { return self.Get("LastName", string.Empty); }
        set { self.Set("LastName", value, false); }
    } 

    public Children Children
    {
        get { return new Children(self.GetElement("Children")); }
    }
}

public class Children
{
    XElement self;
    public Children(XElement children) { self = children; }

    public Child this[string name]
    {
        get 
        { 
            return self.Elements("Name")
                         .Select(x => new Child(x))
                         .FirstOrDefault(c => c.Name == name);
        }
    }

    public Child this[int index]
    {
         get { return new Child(self.Elements("Name").ElementAt(index)); }
    }

    public void Add(string name)
    {
         Child child = this[name];
         if(null == child)
         {
             child = new Child(new XElement("Name"));
             child.Name = name;
             self.Add(child.self);
         }
         else
             throw new ArgumentException("Child with name: "+ name +" already exists!");
    }
}

public class Child
{
    internal XElement self;
    public Child(XElement child) { self = child; }

    // You can have an Age/DOB for the child as well, or remove these two properties.
    // Age should be a value of Now minus DOB in years.
    public int Age { get { return DateTime.Now.Year - DOB.Year; } }

    // DOB is an attribute as self.Value in Name erases all child nodes when set.
    public DateTime DOB
    {
        get { return self.Get("DOB", DateTime.MinValue ); } // choose a default date that works for you
        set { self.Set("DOB", value, true); }
    }

    public string Name
    {
        get { return self.Value }
        set { self.Value = value; }
    } 
}

如果要将 null 指定为任何 Get() 的默认值,则需要指定 Get 的类型Get<Type>("node", null)

于 2012-05-29T16:04:11.443 回答
0

我建议看一下LINQ to XML及其 XDocument 类。它使 xml 文档的导航和编辑变得非常容易。

于 2012-05-29T14:50:17.663 回答
-1

查看此站点以获取 linq to xml 的示例

于 2012-05-29T14:50:57.737 回答