【问题标题】:Add a record to relational XML with C#使用 C# 将记录添加到关系 XML
【发布时间】:2010-12-16 13:46:00
【问题描述】:

我有一个 XML 和 XSD 我想用数据集向 trck 表添加一条记录我也在编写该代码但需要 trackListrow... 我该如何处理?

playListDS rec = new playListDS();
            if(File.Exists(Server.MapPath("~/playlist.xml")))
                rec.ReadXml(Server.MapPath("~/playlist.xml"));


            int id = int.Parse(rec.track.Rows[rec.track.Rows.Count - 1][0].ToString()) + 1;

            if (ViewState["Filename"] != null && ViewState["Cover"] != null)
            {
                playListDS.trackListRow row  = new playListDS.trackListRow();
                  rec.track.AddtrackRow(id.ToString(), "mp3/" + ViewState["Filename"].ToString(), txtartist.Text,
                  txtalbum.Text, txttitle.Text,
                  txtannotation.Text, txtduration.Text, "mp3/cover" + ViewState["Cover"].ToString(),
                  txtinfo.Text, txtlink.Text);

    <?xml version="1.0"?>
<!-- Generated using Flame-Ware Solutions XML-2-XSD v2.0 at http://www.flame-ware.com/Products/XML-2-XSD/ -->
<xs:schema id="playListDS" targetNamespace="http://tempuri.org/playListDS.xsd" xmlns:mstns="http://tempuri.org/playListDS.xsd" xmlns="http://tempuri.org/playListDS.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified">
  <xs:element name="playListDS" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="1">
        <xs:element name="trackList">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="track" minOccurs="0" maxOccurs="unbounded">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element name="FileID" type="xs:string" minOccurs="0" />
                    <xs:element name="location" type="xs:string" minOccurs="0" />
                    <xs:element name="creator" type="xs:string" minOccurs="0" />
                    <xs:element name="album" type="xs:string" minOccurs="0" />
                    <xs:element name="title" type="xs:string" minOccurs="0" />
                    <xs:element name="annotation" type="xs:string" minOccurs="0" />
                    <xs:element name="duration" type="xs:string" minOccurs="0" />
                    <xs:element name="image" type="xs:string" minOccurs="0" />
                    <xs:element name="info" type="xs:string" minOccurs="0" />
                    <xs:element name="link" type="xs:string" minOccurs="0" />
                  </xs:sequence>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

<?xml version="1.0" standalone="yes"?>
<playListDS xmlns="http://tempuri.org/playListDS.xsd">
  <trackList>
    <track>
      <FileID>6</FileID>
      <location>mp3/Gomez - See The World-1.mp3</location>
      <creator>Gomez</creator>
      <album>How We Operate</album>
      <title>See the World</title>
      <annotation>Buraya kendi yorumun gelicek boş kalabilir</annotation>
      <duration>243670</duration>
      <image>mp3/coverChrysanthemum.jpg</image>
      <info />
      <link>Grubun bi sitesi fln varsa buraya yazabilisin yoksa beni sil</link>
    </track>
  </trackList>
</playListDS>

【问题讨论】:

    标签: c# xml xsd dataset


    【解决方案1】:

    是的,但是那样操纵 xml。露mp3播放器看不到两个trackList Table...

         <trackList>
        <track>
          <FileID>6</FileID>
          <location>mp3/Gomez - See The World-1.mp3</location>
          <creator>Gomez</creator>
          <album>How We Operate</album>
          <title>See the World</title>
          <annotation>Buraya kendi yorumun gelicek boş kalabilir</annotation>
          <duration>243670</duration>
          <image>mp3/coverChrysanthemum.jpg</image>
          <info />
          <link>Grubun bi sitesi fln varsa buraya yazabilisin yoksa beni sil</link>
        </track>
      </trackList>
      <trackList>
        <track>
          <FileID>8</FileID>
          <location>mp3/Archives - Sleepdriving.mp3</location>
          <creator>Grand Archives</creator>
          <album>KEXP</album>
          <title>Sleepdriving</title>
          <annotation>Buraya kendi yorumun gelicek boş kalabilir</annotation>
          <duration>323562</duration>
          <image>mp3/coverDesert.jpg</image>
          <info>PITCHFORK / FORKCAST
    http://pitchforkmedia.com</info>
          <link>Grubun bi sitesi fln varsa buraya yazabilisin yoksa beni sil</link>
        </track>
      </trackList>
    

    【讨论】:

    • 您可能想要编辑您的原始帖子,而不是发布您的回复作为答案。请参阅常见问题解答中的如果我没有得到好的答案怎么办?。 stackoverflow.com/faq
    【解决方案2】:

    你的意思是这样的:

    playListDS rec = new playListDS();
    if(File.Exists(Server.MapPath("~/playlist.xml")))
    {
        rec.ReadXml(Server.MapPath("~/playlist.xml"));
    }
    
    int id = int.Parse(rec.track.Rows[rec.track.Rows.Count - 1][0].ToString()) + 1;
    
    if (ViewState["Filename"] != null && ViewState["Cover"] != null)
    {
        playListDS.trackListRow row = rec.track.AddtrackRow(id.ToString(), "mp3/" +
                                  ViewState["Filename"].ToString(), txtartist.Text,
                                  txtalbum.Text, txttitle.Text,
                                  txtannotation.Text, txtduration.Text, "mp3/cover" +
                                  ViewState["Cover"].ToString(),
                                  txtinfo.Text, txtlink.Text);
    }
    

    您只需要新添加的行吗?如果是这样,您可以从 AddtrackRow 方法中获取新行。

    【讨论】:

      猜你喜欢
      • 2011-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多