简体   繁体   中英

How to read XML to create resx file

I have an XML File

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

    // More string Tags here

</resources>

I tried

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");
}

How to read this xml so that I can create a resx File.

I did it finally

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();

Something like this:

        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();
                }
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM