5

我有一个 XML 文件

<? xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="Foo">Bar</string>
    <string name="Foo1">Bar1</string>

    // More string Tags here

</resources>

我试过

XMLTextReader reader = new XmlTextReader("FooBar.xml");

ResXResourceWriter writer = new ResXResourceWriter("FooBar.resx");

while(reader.Read())
{
    if(reader.NodeType == XmlNodeType.Element && reader["name"] != null)
       writer.AddResource("What_should_I_write_here", "What_should_I_write_here");
}

如何读取这个 xml 以便我可以创建一个 resx 文件。

4

2 回答 2

6

我终于做到了

XMLTextReader reader = new XmlTextReader("FooBar.xml");

ResXResourceWriter writer = new ResXResourceWriter("FooBar.resx");

while(reader.Read())
{
    if(reader.NodeType == XmlNodeType.Element && reader.Name == "string")
       writer.AddResource(reader.GetAttribute("name"), reader.ReadString());
}

writer.Generate();
writer.Close();
于 2012-08-04T10:23:44.617 回答
2

像这样的东西:

        var xmlReader = new XmlTextReader("c:\\temporary\\cars.xml");

        while (xmlReader.Read())
        {
            switch(xmlReader.NodeType)
            {
                case XmlNodeType.XmlDeclaration:
                case XmlNodeType.Element:
                case XmlNodeType.Comment:
                    sb.AppendFormat("{0}: {1}={2}", xmlReader.NodeType, xmlReader.Name, xmlReader.Value);
                    sb.AppendLine();
                    break;
                case XmlNodeType.Text:
                    sb.AppendFormat("   - Value: {0}", xmlReader.Value);
                    sb.AppendLine();
                    break;
            }

            if(xmlReader.HasAttributes)
            {
                while(xmlReader.MoveToNextAttribute())
                {
                    sb.AppendFormat("   - Attribute: {0}={1}", xmlReader.Name, xmlReader.Value);
                    sb.AppendLine();
                }
            }
        }
于 2012-08-04T09:24:26.430 回答