none
请教:第一种xml格式是如何生成的。谢谢。 RRS feed

  • 问题

  • 第一种XML格式。

    <?xml version="1.0" encoding="utf-8" ?> <CityList> <City X="160" Y="370"/> <City X="121" Y="132"/> <City X="58" Y="234"/> <City X="41" Y="213"/> <City X="143" Y="86"/> </CityList>

    第二种XML格式(书中看到的xml格式)

    <?xml version="1.0" encoding="utf-8" ?> 
    <Student>
      <Person>
        <Name>小明</Name>
        <Sex>男</Sex>
        <Age>23</Age>
        <Number>123456</Number>
        </Person>
      </Student>

    我在网上找到一个程序,它运行时调取第一种的xml格式文件,但不知它是如何生成的。谢谢。

    杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............

    2013年9月3日 23:02

答案

  • 参考下面例子:

    XmlDocument xmlDoc = new XmlDocument();
                //创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
                xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8",""));
                //创建根节点
                XmlNode rootNode = xmlDoc.CreateElement("CityList");
    
                //创建City子节点
                XmlNode cityNode = xmlDoc.CreateElement("City");
                //创建X属性
                XmlAttribute XAttribute = xmlDoc.CreateAttribute("X");
                XAttribute.Value = "160";
                //创建Y属性
                XmlAttribute YAttribute = xmlDoc.CreateAttribute("Y");
                YAttribute.Value = "370";
                //xml节点附加属性
                cityNode.Attributes.Append(XAttribute);
                cityNode.Attributes.Append(YAttribute);
    
                //创建City子节点
                XmlNode cityNode1 = xmlDoc.CreateElement("City");
                //创建X属性
                XmlAttribute XAttribute1 = xmlDoc.CreateAttribute("X");
                XAttribute1.Value = "121";
                //创建Y属性
                XmlAttribute YAttribute1 = xmlDoc.CreateAttribute("Y");
                YAttribute1.Value = "132";
                //xml节点附加属性
                cityNode1.Attributes.Append(XAttribute1);
                cityNode1.Attributes.Append(YAttribute1);
                // 附加子节点
                rootNode.AppendChild(cityNode);
                rootNode.AppendChild(cityNode1);
                // 附加根节点
                xmlDoc.AppendChild(rootNode);
    
                // 保存XML文档
                xmlDoc.Save(@"d:\test.xml");

    上面代码我已经测试过了,可以生成你的第一种格式的xml文件,具体生成结果为:

    <?xml version="1.0" encoding="utf-8"?>
    <CityList>
      <City X="160" Y="370" />
      <City X="121" Y="132" />
    </CityList>

    最好的学习方式应该是提供一些有用的链接,然后你参考学习来自己实现的,更多关于XML操作的内容参考:

    http://www.cnblogs.com/yukaizhao/archive/2011/07/19/csharp_xmldocument_access_xml.html


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample

    2013年9月4日 5:56
  • Linq to Xml创建这样的xml文件是很简单的,代码如下:

    XDocument cityList = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("This is a comment.Just input what you want to say."),
                    new XElement("CityList",
                            new XElement("City", new XAttribute("X","160"),new XAttribute("Y","370")),
                            new XElement("City", new XAttribute("X","121"),new XAttribute("Y","132")),
                            new XElement("City", new XAttribute("X","58"),new XAttribute("Y","234")),
                            new XElement("City", new XAttribute("X","41"),new XAttribute("Y","213")),
                            new XElement("City", new XAttribute("X","143"),new XAttribute("Y","86"))
                     )
                 );
                XDocument student = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("This is a comment.Just input what you want to say."),
                    new XElement("Student",
                            new XElement("Person", 
                                new XElement("Name","小明"),
                                new XElement("Sex","男"),
                                new XElement("Age","23"),
                                new XElement("Number","123456")
                            )
                     )
                 );
                cityList.Save("CityList.xml");
                student.Save("Student.xml");


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • 已标记为答案 杲大盛 2013年9月5日 1:19
    2013年9月4日 8:23
    版主

全部回复

  • 第一种写的是XmlNode.Attributes

    http://msdn.microsoft.com/zh-cn/library/system.xml.xmlnode.attributes%28v=vs.80%29.aspx


    http://feiyun0112.cnblogs.com/

    2013年9月4日 0:59
    版主
  • 第一种格式通过XmlElement.SetAttribute() 方法来设置属性值,更多XML操作可以参考下面文章:

    http://www.cnblogs.com/whitetiger/archive/2008/08/30/1279904.html


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample

    2013年9月4日 2:37
  • 请举一个实例,谢谢。


    杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............

    2013年9月4日 5:30
  • 请举一个实例,谢谢。

    杲大胜 ...........勤!能!补!拙!........ 勤!勤!勤!.............

    2013年9月4日 5:31
  • 参考下面例子:

    XmlDocument xmlDoc = new XmlDocument();
                //创建Xml声明部分,即<?xml version="1.0" encoding="utf-8" ?>
                xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8",""));
                //创建根节点
                XmlNode rootNode = xmlDoc.CreateElement("CityList");
    
                //创建City子节点
                XmlNode cityNode = xmlDoc.CreateElement("City");
                //创建X属性
                XmlAttribute XAttribute = xmlDoc.CreateAttribute("X");
                XAttribute.Value = "160";
                //创建Y属性
                XmlAttribute YAttribute = xmlDoc.CreateAttribute("Y");
                YAttribute.Value = "370";
                //xml节点附加属性
                cityNode.Attributes.Append(XAttribute);
                cityNode.Attributes.Append(YAttribute);
    
                //创建City子节点
                XmlNode cityNode1 = xmlDoc.CreateElement("City");
                //创建X属性
                XmlAttribute XAttribute1 = xmlDoc.CreateAttribute("X");
                XAttribute1.Value = "121";
                //创建Y属性
                XmlAttribute YAttribute1 = xmlDoc.CreateAttribute("Y");
                YAttribute1.Value = "132";
                //xml节点附加属性
                cityNode1.Attributes.Append(XAttribute1);
                cityNode1.Attributes.Append(YAttribute1);
                // 附加子节点
                rootNode.AppendChild(cityNode);
                rootNode.AppendChild(cityNode1);
                // 附加根节点
                xmlDoc.AppendChild(rootNode);
    
                // 保存XML文档
                xmlDoc.Save(@"d:\test.xml");

    上面代码我已经测试过了,可以生成你的第一种格式的xml文件,具体生成结果为:

    <?xml version="1.0" encoding="utf-8"?>
    <CityList>
      <City X="160" Y="370" />
      <City X="121" Y="132" />
    </CityList>

    最好的学习方式应该是提供一些有用的链接,然后你参考学习来自己实现的,更多关于XML操作的内容参考:

    http://www.cnblogs.com/yukaizhao/archive/2011/07/19/csharp_xmldocument_access_xml.html


    If my post is helpful,please help to vote as helpful, if my post solve your question, please help to make it as answer. My sample

    2013年9月4日 5:56
  • Linq to Xml创建这样的xml文件是很简单的,代码如下:

    XDocument cityList = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("This is a comment.Just input what you want to say."),
                    new XElement("CityList",
                            new XElement("City", new XAttribute("X","160"),new XAttribute("Y","370")),
                            new XElement("City", new XAttribute("X","121"),new XAttribute("Y","132")),
                            new XElement("City", new XAttribute("X","58"),new XAttribute("Y","234")),
                            new XElement("City", new XAttribute("X","41"),new XAttribute("Y","213")),
                            new XElement("City", new XAttribute("X","143"),new XAttribute("Y","86"))
                     )
                 );
                XDocument student = new XDocument(
                    new XDeclaration("1.0", "utf-8", "yes"),
                    new XComment("This is a comment.Just input what you want to say."),
                    new XElement("Student",
                            new XElement("Person", 
                                new XElement("Name","小明"),
                                new XElement("Sex","男"),
                                new XElement("Age","23"),
                                new XElement("Number","123456")
                            )
                     )
                 );
                cityList.Save("CityList.xml");
                student.Save("Student.xml");


    Caillen
    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    • 已标记为答案 杲大盛 2013年9月5日 1:19
    2013年9月4日 8:23
    版主